Girls' research HDU - 3294 (Manacher)

One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps: 
First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef".
Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.

Input

Input contains multiple cases. 
Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'a' is and the length of the string will not exceed 200000.All input must be lowercase. 
If the length of string is len, it is marked from 0 to len-1.

Output

Please execute the operation following the two steps. 
If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!". 
If there are several answers available, please choose the string which first appears.

Sample Input

b babd
a abcd

Sample Output

0 2
aza
No solution!

题目大意:先给出一个字符,表示真的字符a,一次类推,这个字符的下一个字符对应的就是c,题目叙述中有举例子。在输入一个字符串,要求你求出转化之后的字符串中最长回文子串的首位位置及最长回文子串。

解题思路: 现根据给出的转化要求,求出对应的字符串,用Manacher算法即可求出最长回文子串的首位位置及子串本身。

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

char s[Maxn], ch ;
int len ;

void Manacher (){
    string str ;
    str = "$#" ;
    for (int i = 0; i < len; i++){
        str += s[i] ;
        str += "#" ;
    }
//    cout << str << endl ;
    len = str.size() ;
    vector < int > ve (len, 0) ;
    int Mx = 0, id = 0, maxLen = 0, index = 0 ;
    for (int i = 0; i < len; i++){
        ve[i] = Mx > i ? min(ve[2 * id - i], Mx - i) : 1 ;
        while (str[i + ve[i]] == str[i - ve[i]]) ve[i]++;
        if (Mx < i + ve[i]){
            Mx = i + ve[i] ;
            id = i ;
        }
        if (maxLen < ve[i]) {
            maxLen = ve[i] ;
            index = i ;
        }
    }
    if (maxLen - 1 == 1) cout << "No solution!" << endl ;
//    else {
//        cout << (index - maxLen) / 2 << " " << (index - maxLen) / 2 + maxLen - 2 << endl ;
        cout << str.substr((index - maxLen) / 2, maxLen - 1) << endl ;
//        int cnt = 0 ;
//        for (int i = (index - maxLen) / 2 ;i < len; i++){
//            if (str[i] != '$' && str[i] != '#'){
//                cnt++ ;
//                cout << str[i] ;
//            }
//            if (cnt == maxLen - 1) break ;
//        }
//        cout << endl ;
//    }
    else {
        cout << (index - maxLen) / 2 << " " << (index - maxLen) / 2 + maxLen - 2 << endl ;
        for (int i = (index - maxLen) / 2; i < (index - maxLen) / 2 + maxLen - 1; i++){
            cout << s[i] ;
        }
        cout << endl ;
    }
}

int main (){
    ios_base::sync_with_stdio(false) ;
    cin.tie(0) ;
    cout .tie(0) ;
    while (cin >> ch >> s ){
        len = strlen(s) ;
        int sub = ch - 'a' ;
        for (int i = 0; i < len; i++){
            s[i] -= sub ;
            if (s[i] < 'a') s[i] += ('z' - 'a' + 1) ;
        }
//        cout << s << endl ;
        Manacher() ;
    }
    return 0 ;
}

 

你可能感兴趣的:(Manacher,strings)