回文字符串 NYOJ

# include<iostream>
# include<string>
# include<string.h>
# include<queue>
# include<stdio.h>
#include <algorithm>
using namespace std;
int d[1001][1001]; 
int main()
{
      int n,m,i,j;
      cin>>n;
      while(n--)
      {
          char s1[1001],s2[1001];
          scanf("%s",s1);
          int len = strlen(s1)-1;
          for(i=len,j=0;i>=0;i--,j++)
              s2[i] = s1[j];
          
          for(i=0;i<=len;i++)
          {
              for(j=0;j<=len;j++)
            {
                if(s1[i]==s2[j])        
                {
                    if(i==0||j==0)    d[i][j]= 1;
                    else              d[i][j] = d[i-1][j-1] + 1;
                }
                else
                {
                    if(i==0&&j==0)    
                    {
                        d[i][j]= 0;
                        continue;
                    }
                    if(i==0)    
                    {
                        d[i][j]= d[i][j-1];
                        continue;
                    }
                    if(j==0)    
                    {
                        d[i][j]= d[i-1][j];
                        continue;
                    }
                    if(d[i-1][j]>=d[i][j-1])    d[i][j] = d[i-1][j];
                    else                        d[i][j] = d[i][j-1];    
            }    
            }
        }
          printf("%d\n",len-d[len][len]+1);
    }
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/fzuhyj/p/9482065.html