UVA 11022 Solution - String Factoring

/*Solved by using dynamic programming*/

/*
UVA problem ID: 11022String Factoring
uva id: shoaib05
Accepted Time: 0.025
*/

#include<iostream>
#include<string>

using namespace std;
int main()
{
freopen("in.txt","r",stdin);
string str,templ,tempr,temp,tt;
int i,j,k,x,len,table[85][85],min,l,mint,ltemp,index;
bool flag;
while(cin>>str)
{
if(str[0]=='*')
break;
len=str.size();
len--;
for(i=0;i<=len;i++)
table[i][i]=1;
x=1;
while(x<=len)
{
for(i=0,j=x;j<=len;i++,j++)
{
l=1;
min=10000;
flag=false;
for(k=i;k<j;k++)
{
templ=str.substr(i,l);
l++;
if(table[i][k]==table[k+1][j])
{
ltemp=templ.size();
for(index=i+ltemp;index<=j;index=index+ltemp)
{
flag=true;
tt=str.substr(index,ltemp);
{
if(templ!=tt)
{
flag=false;
break;
}
}
}
}
if(flag)
{
min=table[i][k];
break;
}
mint=table[i][k]+table[k+1][j];
min=min<mint?min:mint;
}
table[i][j]=min;
}
x++;
}
cout<<table[0][len]<<endl;
}
return 0;
}

Comments

Popular posts from this blog

uva 679 - Dropping Balls Solution

uva 481 - What Goes Up Solution

uva-10077 Solution --- The Stern-Brocot Number System