回文字符串
时间限制:3000 ms | 内存限制:65535 KB
难度:4
描述
所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如”aba”。当然,我们给你的问题不会再简单到判断一个字符串是不是回文字符串。现在要求你,给你一个字符串,可在任意位置添加字符,最少再添加几个字符,可以使这个字符串成为回文字符串。
输入
第一行给出整数N(0
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<iomanip>
using namespace std;
const int maxn=1005;
int DP[maxn][maxn];
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
string str;
cin>>str;
string copy_str=str;
reverse(copy_str.begin(),copy_str.end());
for(int i=0; i<(int)str.size(); i++)
for(int j=0; j<(int)copy_str.size(); j++)
str[i]==copy_str[j]?DP[i+1][j+1]=DP[i][j]+1:DP[i+1][j+1]=max(DP[i][j+1],DP[i+1][j]);
printf("%d\n",(int)str.size()-DP[(int)str.size()][(int)str.size()]);
}
return 0;
}