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

21 篇文章 0 订阅
6 篇文章 0 订阅
给定一个可能非回文的字符串,只允许在右侧添加字符使其变为回文,目标是最小化回文串长度。提供Manacher算法和KMP算法两种解决方案。样例输入和输出展示了不同情况下的最短回文串长度。
摘要由CSDN通过智能技术生成

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值