#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int disc[1010][1010]={0};
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int t;
int i,j;
char str1[1001];
char str2[1001];
scanf("%d",&t);
while(t--)
{
scanf("%s",str1);
strcpy(str2,str1);
reverse(str2,str2+strlen(str2));
for(i=1;i<=strlen(str1);i++)
{
for(j=1;j<=strlen(str2);j++)
{
if(str1[i-1]==str2[j-1])
{
disc[i][j]=disc[i-1][j-1]+1;
}else{
disc[i][j]=max(disc[i-1][j],disc[i][j-1]);
}
}
}
printf("%d\n",strlen(str1)-disc[strlen(str1)][strlen(str2)]);
}
return 0;
}
nyoj 37 回文字符串
最新推荐文章于 2019-07-16 09:56:10 发布
本文介绍了一个通过比较字符串与其反转后的字符串来寻找最长回文子序列的算法实现。该算法利用动态规划的方法,计算出原始字符串与反转字符串之间的最长公共子序列,进而求得原字符串中最长的回文子序列长度。
778

被折叠的 条评论
为什么被折叠?



