标签:
1 #include<stdio.h> 2 #include<string.h> 3 const int MAXN=5010; 4 const int INF=0xfffffff; 5 #define MAX(x,y) (x>y?x:y) 6 int dp[MAXN][2]; 7 char a[MAXN],b[MAXN]; 8 int main(){ 9 int t,i,j; 10 while(~scanf("%d",&t)){ 11 scanf("%s",a+1); 12 //printf("%d\n",t); 13 for(i=t,j=1;i>0;i--,j++)b[j]=a[i]; 14 b[j]=‘\0‘; 15 //printf("%s\n",b+1); 16 memset(dp,0,sizeof(dp)); 17 dp[0][0]=0; 18 for(i=1;i<=t;i++){ 19 for(j=1;j<=t;j++){ 20 if(a[i]==b[j])dp[j][i%2]=dp[j-1][(i-1)%2]+1; 21 else dp[j][i%2]=MAX(dp[j-1][i%2],dp[j][(i-1)%2]); 22 } 23 } 24 //printf("%d\n",dp[t][t%2]); 25 printf("%d\n",t-dp[t][t%2]); 26 } 27 return 0;}
题解;还可以是%2,%3,%4,........
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4719360.html