1258 - Making Huge Palindromes
PDF (English) Statistics Forum
Time Limit: 1 second(s) Memory Limit: 32 MB
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
Output for Sample Input
4

bababababa

pqrs

madamimadam

anncbaaababaaa

Case 1: 11

Case 2: 7

Case 3: 11

Case 4: 19

 

给出一个字符串,求得要在最后加几个字符能组成最短的回文字符串,输出这个回文字符串的长度

把字符翻转过来,当成模板串,然后再和原串进行匹配,到最后的时候,求出匹配的长度,这个长度也就是在原串中已经构成回文的那部分的最大长度j,然后len-j 是不能匹配的长度,加上len 得到处理后的最短的回文字符串的长度了

 

例如:

12122   

想把它转化为回文的,那么先求出翻转后的字符串

22121

然后就是找原串的后缀和翻转串的前缀的最多匹配的个数了,kmp跑完之后,j就是匹配的个数,本例即为 22 

那就把翻转串的剩下的字符串(长度为len-j)连接在原串上,就得到了最后的回文串结果,长度为len-j+len,也就是2*len-j

证明完毕!

 

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <stdbool.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <string.h>
 8 #include <math.h>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <set>
13 #include <map>
14 
15 #define INF 0x3f3f3f3f
16 #define LL long long
17 #define MAXN 1000005
18 #define mod 1000000007
19 using namespace std;
20 
21 char S[MAXN],T[MAXN];
22 int nexts[MAXN];
23 
24 
25 
26 void GetNext(char *str)
27 {
28     int i=0,j=-1;
29     nexts[0] = -1;
30     int len = strlen(str);
31     while (i<len)
32     {
33         if (j == -1 || str[i]==str[j])
34         {
35             i++;
36             j++;
37             nexts[i] = j;
38         } else
39             j = nexts[j];
40     }
41 }
42 
43 int KMP(char *S,char *T)
44 {
45     GetNext(T);
46     int j = 0;
47     int i = 0;
48     int len_s = strlen(S);
49     int len_t = strlen(T);
50     int cnt = 0;
51     while (i<len_s)
52     {
53         if (j==-1 || S[i]==T[j])
54         {
55             i++;
56             j++;
57         }
58         else
59             j = nexts[j];
60     }
61     return 2*i-j;
62 }
63 
64 int main()
65 {
66     int t;
67     scanf("%d",&t);
68     int k=1;
69     while (t--)
70     {
71         scanf("%s",S);
72         getchar();
73         int len = strlen(S);
74         for (int i=0;i<len;i++)
75             T[i] = S[len-i-1];
76         printf("Case %d: %d\n",k++,KMP(S,T));
77     }
78     return 0;
79 }