D - 楼下水题(kmp+Manacher)

D - 楼下水题
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;

代码:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<map>
 7 #include<vector>
 8 #include<stack>
 9 #include<queue>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 const int MAXN=1000010;
13 char s[MAXN],m[MAXN];
14 int p[MAXN];
15 void getp(){
16     int i=0,j=-1;
17     p[0]=-1;
18     while(m[i]){
19         if(j==-1||m[i]==m[j]){
20             i++;j++;
21             p[i]=j;
22         }
23         else j=p[j];
24     }
25 }
26 int kmp(){
27     getp();
28    // for(int i=0;s[i];i++)printf("%d ",p[i]);puts("");
29     int i=0,j=0;
30     int mx=0,temp=0;
31     while(m[i]){
32         if(j==-1||m[j]==s[i]){
33             i++;j++;
34         }
35         else{
36             j=p[j];
37         }
38     }
39     return j;
40 }
41 int main(){
42     int T,flot=0;
43     scanf("%d",&T);
44     while(T--){
45         scanf("%s",s);
46         strcpy(m,s);
47         int len=strlen(s);
48         reverse(m,m+len);
49     //    puts(m);
50         //puts(s);
51         printf("Case %d: %d\n",++flot,len+len-kmp());
52     }
53     return 0;
54 }

 Manacher:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<vector>
 7 #define mem(x,y) memset(x,y,sizeof(x))
 8 using namespace std;
 9 typedef long long LL;
10 const int INF=0x3f3f3f3f;
11 const int MAXN=1000010;
12 char str[MAXN],s[MAXN<<1];
13 int p[MAXN<<1],Ms;
14 int Manacher(char *s,int len){
15     mem(p,0);
16     p[0]=p[1]=1;
17     int id=1,mx=1,ans=0;
18     Ms=0;
19     for(int i=2;i<len;i++){
20         if(mx>i)p[i]=min(p[2*id-i],mx-i);//2*id-i是对称轴左侧;
21         else p[i]=1;
22         while(s[i-p[i]]==s[i+p[i]])p[i]++;
23         if(p[i]+i>mx)mx=p[i]+i,id=i;
24         ans=max(ans,p[i]); 
25         if(mx==len)Ms=max(Ms,p[i]-1);
26     }
27     return ans-1;
28 }
29 int main(){
30     int T,flot=0;
31     scanf("%d",&T);
32     while(T--){
33         scanf("%s",str);
34         int len=strlen(str);
35         s[0]='@';
36         for(int i=0;i<len;i++){
37             s[2*i+1]='#';
38             s[2*i+2]=str[i];
39         }
40         s[2*len+1]='#';
41         int t=Manacher(s,2*len+2);
42         if(t==len)printf("Case %d: %d\n",++flot,len);
43         else printf("Case %d: %d\n",++flot,len+len-Ms);
44     }
45     return 0;
46 }

 

#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;
}

  

posted @ 2015-10-24 22:10  handsomecui  阅读(337)  评论(0编辑  收藏  举报