Extend to Palindrome SPOJ - EPALIN 哈希或KMP

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

Your task is, given an integer N, to make a palindrome (word that reads the same when you reverse it) of length at least N (1 <= N <= 100,000). Any palindrome will do.

Easy, isn't it? That's what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn't solved. The problem with the code is that the strings generated are often not palindromic. There's not enough time to start again from scratch or to debug his messy code.

Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end. The input string will consist of only upper and lower case letters.

Example

Input:
aaaa
abba
amanaplanacanal
xyz

Output:
aaaa
abba
amanaplanacanalpanama
xyzyx

Note: 
1. All palindromes are considered case-sensitive (i.e. 'Aa' is not a palindrome). 
2. Large I/O. Be careful in certain languages.

题意:在原串尾添加尽量少字符使之成为回文串。

我用了哈希和KMP两种写法

哈希思路: 原串从后往前求hash值,与从前往后对比hash值即可。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e5 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const int seed = 133 ;

string s ;

int main (){
    while (cin >> s ){
        int ans = 0 ;
        int len = s.size() ;
        int pre = 0, tail = 0 ,p = 1 ;
        for (int i = len - 1; i >= 0; i--){
            pre = pre + p * (s[i] - 'a') ;
            tail = tail * seed + (s[i] - 'a') ;
            p *= seed ;
            if (pre == tail ) ans = i ;
        }
        cout << s ;
        for (int i = ans - 1; i >= 0; i--) cout << s[i] ;
        cout << endl ;
    }
    return 0 ;
}

KMP思路:把原串颠倒过来,将颠倒后的串作为模式串与原串进行匹配,求最长回文位置,最后输出kmp返回的下标到最后即可。

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 1e5 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const int seed = 133 ;

string s, str ;
int Next[Maxn], len;

void Get_Next(){
    Next[0] = -1 ;
    int k = -1, j = 0 ;
    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 || str[i] == s[j]){
            i++ ;
            j++ ;
        }
        else j = Next[j] ;
    }
    return j ;
}

int main (){
    while (cin >> s){
        len = s.size() ;
        str = s ;
        reverse(s.begin(), s.end()) ;
//        cout << s << endl ;
        Get_Next() ;
        int index = KMP() ;
//        cout << index << endl ;
        cout << str ;
        for (int i = index; i < len; i++){
            cout << s[i] ;
        }
        cout << endl ;
    }
    return 0 ;
}

盈盈一水间

脉脉不得语

猜你喜欢

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