Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not.
Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.
For example, the string is 'bababa'. You can make many palindromes including
bababababab
babababab
bababab
Since we want a palindrome with minimum length, the solution is 'bababab' cause its length is minimum.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.
Output
For each case, print the case number and the length of the shortest palindrome you can make with S.
Sample Input
4
bababababa
pqrs
madamimadam
anncbaaababaaa
Sample Output
Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19
题解:
kmp,找右侧添加若干字母使这个字符串回文;也可以用manacher算法,ans用来找最大回文串长度,Ms用来找从可以到字符串结尾的回文串长度;
少输出了换行没有pe,是wa。。。无耐;
记住要是反转后的串匹配未旋转的串;
anncbaaababaaa
aaababaaacnna
最后的p指向失配的位置9;所以是len+len-j;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
char s[MAXN],m[MAXN];
int p[MAXN];
void getp(){
int i=,j=-;
p[]=-;
while(m[i]){
if(j==-||m[i]==m[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
int kmp(){
getp();
// for(int i=0;s[i];i++)printf("%d ",p[i]);puts("");
int i=,j=;
int mx=,temp=;
while(m[i]){
if(j==-||m[j]==s[i]){
i++;j++;
}
else{
j=p[j];
}
}
return j;
}
int main(){
int T,flot=;
scanf("%d",&T);
while(T--){
scanf("%s",s);
strcpy(m,s);
int len=strlen(s);
reverse(m,m+len);
// puts(m);
//puts(s);
printf("Case %d: %d\n",++flot,len+len-kmp());
}
return ;
}
Manacher:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=;
char str[MAXN],s[MAXN<<];
int p[MAXN<<],Ms;
int Manacher(char *s,int len){
mem(p,);
p[]=p[]=;
int id=,mx=,ans=;
Ms=;
for(int i=;i<len;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);//2*id-i是对称轴左侧;
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])p[i]++;
if(p[i]+i>mx)mx=p[i]+i,id=i;
ans=max(ans,p[i]);
if(mx==len)Ms=max(Ms,p[i]-);
}
return ans-;
}
int main(){
int T,flot=;
scanf("%d",&T);
while(T--){
scanf("%s",str);
int len=strlen(str);
s[]='@';
for(int i=;i<len;i++){
s[*i+]='#';
s[*i+]=str[i];
}
s[*len+]='#';
int t=Manacher(s,*len+);
if(t==len)printf("Case %d: %d\n",++flot,len);
else printf("Case %d: %d\n",++flot,len+len-Ms);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=1e6+100;
char mstr[MAXN];
int p[MAXN];
char s[MAXN];
void getp(){
int i=0,j=-1;
p[0]=-1;
while(s[i]){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
} int kmp(){
getp();
int j=0,i=0;
while(mstr[i]){
if(j==-1||s[j]==mstr[i]){
i++;j++;
}
else j=p[j];
}
return j;
} int main(){
int kase=0,T;
SI(T);
while(T--){
scanf("%s",s);
strcpy(mstr,s);
int len=strlen(s);
reverse(s,s+len);
printf("Case %d: %d\n",++kase,len+len-kmp());
}
return 0;
}