题目大意就是用字符来代替一段字符串,使改变后的字符串为回文串,求最大长度。
只要查看字符串两边的子串是否相同,相同就把这两个相同的子串变成一个符号,用string会快很多,用hash也行
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
string str1,str2,str3;
int main()
{
int T,ans;
cin>>T;
for(int t=1;t<=T;t++)
{
ans=0;
cin>>str1;
str2="";//空串
str3="";//空串
int len=str1.length();
for(int i=0;i<len/2;i++)
{
str2=str2+str1[i];
str3=str1[len-i-1]+str3;//也可以写成str3+str1[len/2+i],不过要判断len的奇偶性
if(str2==str3)//相等就换成字符
{
str2=str3="";//清空
ans+=2;
}
}
if(str2!=""&&len%2==0) ans++;
if(len%2) ans++;
printf("Case #%d: %d\n",t,ans);
}
return 0;
}