标签:style blog http io ar color os sp for
1 Ab3bd
2
思路:将目标串逆序,求与原串最长公共子序列长度。用目标串长度减去最长公共子序列长度即为所求。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[1002][1002];
int main()
{
char a[1002],s[1002];
int len,i,j,test;
scanf("%d",&test);
while(test--)
{
memset(dp,0,sizeof(dp));
scanf("%s",a);
len=strlen(a);
for(i=0,j=len-1;i<len;i++,j--)
{
s[i]=a[j];
}
for(i=1;i<=len;i++)
{
for(j=1;j<=len;j++)
{
if(a[i-1]==s[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
printf("%d\n",len-dp[len][len]);
}
return 0;
}
标签:style blog http io ar color os sp for
原文地址:http://blog.csdn.net/hdd871532887/article/details/41804847