NYOJ 37 回文字符串

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1010;
int dp[maxn][maxn];
#define Max(a,b) a>b?a:b
int main()
{
  int ncase;
  scanf("%d\n",&ncase);
  char str[maxn],str2[maxn];
  while(ncase--)
  {
    scanf("%s",str);
    int len=strlen(str);
    strcpy(str2,str);
    reverse(str2,str2+len);
    for(int k=0;k<=len;k++)
        dp[k][0]=0;
    for(int m=0;m<=len;m++)
        dp[0][m]=0;
    for(int i=1;i<=len;i++)
    for(int j=1;j<=len;j++)
    {
      if(str[i-1]==str2[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]);
  }
}

posted @ 2015-07-03 21:26  小松鼠。  阅读(103)  评论(0编辑  收藏  举报