Making Huge Palindromes LightOJ - 1258 (Manacher || KMP )

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/89508140

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

Note

Dataset is huge, use faster I/O methods.

题目大意:在一个字符串的最右端添加字符,使得这个字符串为长度最小的回文串,输出最小的长度。

解题思路: 用Manacher算法求出最靠近右端点的回文串的最大长度maxLen,结果就是2 * 字符串长度 - maxLen。

  还有一种解法就是KMP,把原串作为文本串,它的翻转的字符串作为模式串,用模式串在文本串跑KMP,跑完了之后看跑到了模式串的哪个位置定义为j,然后需要添加的字符的数量就是len - j,再加上原串的长度len,结果就是2 * len - j了。

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1258

Manacher解法: 

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std ;
typedef long long ll ;
typedef pair < ll, ll > P ;
const int Maxn = 1e6 + 10 ;
const int INF = 1e18 ;

char s[Maxn], str[Maxn << 1] ;
int len ;

int Manacher(){
    len = strlen(s) ;
    str[0] = '$', str[1] = '#' ;
    int k = 2 ;
    for (int i = 0; i < len; i++){
        str[k++] = s[i] ;
        str[k++] = '#' ;
    }
//    cout << str << endl ;
    vector < int > ve(k) ;
    int maxLen = 0, id = 0, Mx = 0, index = 0 ;
    for (int i = 0; i < k; i++){
        ve[i] = Mx > i ? min(ve[2 * id - i], Mx - i) : 1 ;
        while (str[ve[i] + i] == str[i - ve[i]]) ve[i]++ ;
        if (Mx < i + ve[i]){
            Mx = i + ve[i] ;
            id = i ;
        }
        if (ve[i] + i >= k) {
            maxLen = max(maxLen, ve[i]) ;
        }
    }
//    cout << s.substr((index - maxLen) / 2) << endl ;
    return maxLen - 1 ;
}


int main (){
    int T ;
    scanf("%d", &T) ;
    int Cas = 0 ;
    while (T--){
        scanf("%s", s) ;
        int ans = Manacher() ;
        cout << "Case " << ++Cas << ": " << 2 * strlen(s) - ans << endl ;
    }
    return 0 ;
}

KMP解法:

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
using namespace std ;
typedef long long ll ;
typedef pair < ll, ll > P ;
const int Maxn = 1e6 + 10 ;
const int INF = 1e18 ;

char s[Maxn], str[Maxn] ;
int len, Next[Maxn], k ;

void getNext(){
    Next[0] = -1;
    int j = 0, k = -1 ;
    while (j < len) {
        if (k == -1 || s[j] == s[k]) Next[++j] = ++k ;
        else k = Next[k] ;
    }
}

int KMP (){
    int i = 0, j = 0 ;
    while (i < len){
        if (j == -1 || s[j] == str[i]) {
            i++ ;
            j++ ;
        }
        else j = Next[j] ;
    }
    return j ;
}

int main (){
    int T ;
    scanf("%d", &T) ;
    int Cas = 0 ;
    while (T--){
        memset(Next, 0, sizeof(Next)) ;
        memset(str, 0, sizeof(str)) ;
        scanf("%s", s) ;
        k = 0 ;
        len = strlen(s) ;
        for (int i = 0; i < len; i++) {
            str[k++] = s[i] ;
        }
        str[k++] = '\0' ;
        reverse(s, s + len) ;
//        cout << str << endl << s << endl ;
        getNext() ;
        int ans = KMP() ;
        cout << "Case " << ++Cas << ": " << 2 * len - ans << endl ;
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/89508140