UVA 10617 Solution - Again Palindrome
/* Solved by using dynamic programming*/
/*
uva-10617 -Again Palindrome
uva id: shoaib05
Accepted Time: 0.000
*/
#include<stdio.h>
#include<string.h>
int main()
{
long long table[65][65];
char str[65];
int test,i,j,k,l,len,m;
scanf("%d",&test);
while(test--)
{
scanf("%s",str);
len=strlen(str);
len--;
table[0][0]=1;
for(i=1;i<=len;i++)
{
table[i][i]=1;
table[i][i-1]=0;
}
k=1;
while(k<=len)
{
for(i=0,j=k;j<=len;i++,j++)
{
table[i][j]=table[i][j-1]+1;
for(m=i;m<j;m++)
{
if(str[m]==str[j])
table[i][j]=table[i][j]+table[m+1][j-1]+1;
}
}
k++;
}
printf("%lld\n",table[0][len]);
}
return 0;
}
Comments
Post a Comment