题目是说给你一个字符串,现在要你用一些特殊的符号代替这个字符串中某一些子串,使得被替换后的串是一个回文串。
现在要你求替换后的字符串的最大的可能的长度。
其实这个题目没有什么固定的算法哦,我直接暴力就过了,但是中间手滑,wa了太多发。
其实可以这样来考虑,我们这个字符串的反序也保存一遍,这样可以建立起一个类似于链表的结构(对于每一个字符,我们在这里指向它下一次出现的下标)
这样如果最后可以被替换为n个串,那么一定存在x1,x2,……,xn,是的1-x1,x1-x2,……xn-1-xn是完美的反向匹配的。
同时我们需要维护每一个x最小,这样就一定是最小的。
虽然可能出现极限数据使得复杂度为n^2,但是……(可能卡到n方吗?)
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 100100
using namespace std; int next[][maxn][];
char s[][maxn];
int n,m,k,t,pos[],ans,cas=; void init_Z(int x)
{
memset(pos,0x3f,sizeof pos);
for (int i=n; i>; i--)
{
pos[s[x][i]-'A']=i;
for (int j=; j<; j++)
next[x][i][j]=pos[j];
}
} int match(int x)
{
if (x>n/)
{
if ((n&) && x==n/+) return ;
else return ;
}
int pos=max(next[][x][s[][x]-'A'],next[][x][s[][x]-'A']);
while ()
{
if (pos>n/) return ;
bool flag=true;
for (int i=; i<pos-x+; i++)
if (s[][x+i]!=s[][pos-i])
{
flag=false;
break;
}
if (flag)
return +match(pos+);
else pos=max(next[][pos+][s[][x]-'A'],next[][pos+][s[][x]-'A']);
} } int main()
{
scanf("%d",&t);
while (t--)
{
scanf("%s",s[]+);
n=strlen(s[]+);
for (int i=; i<=n; i++) s[][i]=s[][n+-i];
init_Z(); init_Z();
ans=match();
printf("Case #%d: %d\n",++cas,ans);
}
return ;
}