【ural1297】Palindrome 后缀数组

Description

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.

Sample Input

ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA

Sample Output

ArozaupalanalapuazorA

Source

Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)

求最长回文串。

回文串。回文自动机是啥没听说过呢…

正串和反串中间用奇怪的字符连起来,然后后缀数组,对高度数组做RMQ,找到一个点在正串和反串在新串的位置,查询它们在lcp数组中的区间最小…就这个意思,细节很恶心

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

const int SZ = 1000010;

int n;

int sa[SZ],lcp[SZ],k = 1,rank[SZ],tmp[SZ];

bool cmp_sa(int i,int j)
{
    if(rank[i] != rank[j]) return rank[i] < rank[j];
    else
    {
        int x = i + k <= n ? rank[i + k] : -1;
        int y = j + k <= n ? rank[j + k] : -1;
        return x < y;
    }
}

void get_sa(char s[])
{
    for(int i = 0;i <= n;i ++)
    {
        sa[i] = i;
        rank[i] = i == n ? -1 : s[i];
    }
    for(k = 1;k <= n;k <<= 1)
    {
        sort(sa,sa + 1 + n,cmp_sa);

        tmp[sa[0]] = 0;
        for(int i = 1;i <= n;i ++)
            tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1],sa[i]) ? 1 : 0);
        for(int i = 0;i <= n;i ++)
            rank[i] = tmp[i];
    }
}

void get_lcp(char s[])
{
    int h = 0;
    lcp[0] = 0;
    for(int i = 0;i <= n;i ++)
        rank[sa[i]] = i;
    for(int i = 0;i < n;i ++)
    {
        int j = sa[rank[i] - 1];
        if(h) h --;
        while(i + h < n && j + h < n)
        {
            if(s[i + h] == s[j + h]) h ++;
            else break;
        }
        lcp[rank[i] - 1] = h;
    }
}

int st[SZ][30];

void get_st(int num[],int n)
{
    for(int i = 1;i <= n;i ++) 
        st[i][0] = lcp[i];
    for(int j = 1;j <= log2(n);j ++)
    {
        for(int i = 1;i <= n;i ++)
        {
            st[i][j] = min(st[i][j - 1],st[i + (1 << (j - 1))][j - 1]);
        }
    }
}

int get_min(int l,int r)
{
    r --;
    int k = log2(r - l + 1);
    return min(st[l][k],st[r - (1 << k) + 1][k]);
}

char s[SZ];

int pos = 0,maxl = 0;

void get_ans(int len)
{

    //偶 
    for(int i = 1;i < len;i ++)
    {
        int j = 2 * len - i + 1;
        int l = get_min(min(rank[i],rank[j]),max(rank[i],rank[j]));
// printf("%s %s\n",s + i,s + j); 
// printf("rank : %d %d\n",rank[i],rank[j]); 
// printf("l : %d\n",l); 
// printf("%d %d\n",i,j); 
        if(2 * l > maxl)
        {
            maxl = 2 * l;
            pos = i - l;
        }
    }

    //奇 
    for(int i = 0;i < len;i ++)
    {
        int j = 2 * len - i;
        int l = get_min(min(rank[i],rank[j]),max(rank[i],rank[j]));
// printf("%s %s\n",s + i,s + j); 
        if(2 * l - 1 > maxl)
        {
            maxl = 2 * l - 1;
            pos = i - l + 1;
        }   
    }   
}

/* ABAC$CABA */



int main()
{
    scanf("%s",s);
    int len = strlen(s);
    n = strlen(s);
    s[n] = '$';
    for(int i = n + 1;i <= 2 * n;i ++)
    {
        s[i] = s[2 * n - i];
    } 
    n = n * 2 + 1;
    get_sa(s);
    get_lcp(s);
    get_st(lcp,n - 1);

/* printf("%s\n",s); for(int i = 0;i <= n;i ++) printf("%d %s\n",i,s + sa[i]); puts(""); for(int i = 0;i <= n;i ++) printf("%d ",rank[i]); puts(""); for(int i = 1;i <= n;i ++) printf("%d ",lcp[i]); puts(""); for(int i = 1;i <= n;i ++) { for(int j = 0;j <= 10;j ++) printf("%d ",st[i][j]); puts(""); }*/

    get_ans(len);
    s[pos + maxl] = '\0';
    printf("%s\n",s + pos);
    return 0;
}
/* ABAC */

你可能感兴趣的:(【ural1297】Palindrome 后缀数组)