Palindrome URAL - 1297 后缀数组求最长回文子串

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

The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlimited» has infiltrated into “U.S. Robotics”. «U.S. Robots» security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret).

Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design.

So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards. 
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property.

Your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. As NPRx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.

Input

The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.

Output

The longest substring with mentioned property. If there are several such strings you should output the first of them.

Example

input output
Kazak
aza

题目大意:就是求给出的字符串中最长的回文子串。

解题思路:穷举每一位,然后计算以字符为中心的最长回文子串。注意要分两种情况,一是回文子串的长度为奇数,二是长度为偶数。两种情况都可以转化为求一个后缀和一个反过来写的后缀的最长公共前缀。具体做法是:将整个字符串反过来写在元字符串后面,中间用一个特殊的字符隔开。这样就把问题变成了求这个新的字符串的某两个后缀的最长公共前缀。时间复杂度为(nlogn)。

不是很清楚的是,中间加的特殊字符必须是大于‘z’ 的,不是的话就是wa。。。

实在不明白的,可以把代码中注释部分还原,脑跑一下程序。

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

int sa[Maxn],Rank[Maxn],height[Maxn];
int wa[Maxn],wb[Maxn],Ws[Maxn],wv[Maxn];
char str[Maxn];

int cmp (int *r,int a,int b,int l){
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void get_sa (int *r,int n,int m){
    int i , j, p , *x = wa,*y = wb,*t;
    for (i = 0; i < m; i++) Ws[i] = 0;
    for (i = 0; i < n; i++) Ws[x[i] = r[i]]++;
    for (i = 1; i < m; i++) Ws[i] += Ws[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
    for (j = 1, p = 1; p < n; j *= 2, m = p){
        for (p = 0,i = n - j; i < n; i++) y[p++] = i;
        for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j ;
        for (i = 0; i < n; i++) wv[i] = x[y[i]];
        for (i = 0; i < m; i++) Ws[i] = 0;
        for (i = 0; i < n; i++) Ws[wv[i]]++;
        for (i = 0; i < m; i++) Ws[i] += Ws[i - 1];
        for (i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
        for (t = x, x = y, y = t,p = 1, x[sa[0]] = 0, i = 1; i < n ; i++){
            x[sa[i]] = cmp(y,sa[i - 1],sa[i],j) ? p - 1 : p++;
        }
    }
}
void get_height(int *r,int n){
    int k = 0, j ;
    for (int i = 1 ;i <= n; i++) Rank[sa[i]] = i;
    for (int i = 0; i < n; height[Rank[i++]] = k){
        for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; k++);
    }
}

int a[Maxn] ;

int main (){
    char s[Maxn] ;
    while (~scanf("%s",str)){
        memset(s, 0, sizeof(s)) ;
        memset(a, 0, sizeof(a)) ;
        int n = strlen(str) ;
        for (int i = 0; i < n; i++) s[i] = str[i] ;
        s[n] = '{' ;
        int k = 1 ;
        for (int i = n - 1; i >= 0; i--) {
            s[n + k] = str[i] ;
            k++ ;
        }
//        cout << s << endl ;
        int len = strlen(s) ;
//        cout << "len = " << len << endl ;
        int m = 300;
        for (int i = 0; i < len; i++) a[i] = s[i] ;
        get_sa(a,len + 1,m);
        get_height(a,len);
        int ans = 0 , l = 0, r = 0 , maxLen = 1 ;
//        for (int i = 0; i <= len; i++){
//            printf("sa[%d] = %d\n", i, sa[i]) ;
//        }
//        for (int i = 1; i <= len; i++){
//            cout << (s + sa[i]) << " " << sa[i] << endl ;
//        }
        for (int i = 1; i <= len; i++){
            l = min(sa[i], sa[i - 1]) ;
            r = max(sa[i], sa[i - 1]) ;
//            printf("height[%d] = %d\n", i, height[i]) ;
//            printf("l = %d      r = %d\n", l, r) ;
            if ((l < n && r > n) && len - r == height[i] + l){
                if (height[i] > maxLen){
                    ans = l ;
                    maxLen = height[i] ;
                }
                else if (height[i] == maxLen) ans = min(ans, l) ;
            }
//            cout << "ans = " << ans << " " << "maxLen = "  << maxLen << endl ;
//            cout << endl ;
        }
        for (int i = ans, j = 0; j < maxLen; j++, i++){
            printf("%c", str[i]) ;
        }
        puts("") ;
    }
    return 0;
}

猜你喜欢

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