Light OJ 1258 Making Huge Palindromes KMP构造回文串
KMP   发布于 2019-07-21   400人围观  0条评论
KMP   发表于 2019-07-21   400人围观  0条评论

## 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
anncbaaababaaa```

## Sample Output

```Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19```

## 代码

```#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int T;
int len;
const int maxn = 1e6+5;
char s[maxn],sp[maxn];
int nxt[maxn];
void KMP_pre(){
int j = 0;
nxt[1] = 0;
for(int i = 2;i <= len;++i){
while(j && sp[j+1] != sp[i])j = nxt[j];
if(sp[j+1] == sp[i])j++;
nxt[i] = j;
}
return;
}
int ans = 0;
void KMP(){
int j = 0;
for(int i = 1;i <= len;++i){
while(j && sp[j+1] != s[i])j = nxt[j];
if(sp[j+1] == s[i])j++;
}
ans = j;
return;
}
int main(){
scanf("%d",&T);
getchar();
for(int cas = 1;cas <= T;++cas){
scanf("%s",s+1);
len = strlen(s+1);
for(int i = 1;i <= len;++i)sp[i] = s[i];
reverse(sp+1,sp+len+1);
KMP_pre();
ans = 0;
KMP();
printf("Case %d: %d\n",cas,len*2 - ans);
}
return 0;
}```

0 条评论