#include<stdio.h> #include<string.h> #include<iostream> using namespace std; #define max(a,b) a>b?a:b int f[1001][1001]; char a[1001]; int n; void dp() { int i,j; for(i=0;i<n;i++) for(j=n-1;j>=0;j--) if(a[i]==a[j])f[i+1][n-j]=f[i][n-j-1]+1; else f[i+1][n-j]=max(f[i][n-j],f[i+1][n-j-1]); } int main() { int T; int i; scanf("%d",&T); while(T--) { memset(f,0,sizeof(f)); cin>>a; n=strlen(a); dp(); printf("%d\n",n-f[n][n]); } return 0; }
#include <stdio.h> #include <string.h> char s[1005]; int log[1005][1005]; int main() { int T,n,i,len; scanf("%d",&T); getchar(); while(T--) { gets(s); len=strlen(s); for(i=0;i<len;i++) { log[0][i]=0; log[1][i]=0; } for(n=2;n<=len;n++) { for(i=0;i+n-1<len;i++) { if(s[i]==s[i+n-1]) { log[n][i]=log[n-2][i+1]; } else if(log[n-1][i]<log[n-1][i+1]) { log[n][i]=log[n-1][i]+1; } else { log[n][i]=log[n-1][i+1]+1; } } } printf("%d\n",log[len][0]); } return 0; }