ural 1297 Palindrome(最长回文子串)

Palindrome
Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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

input output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA


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


AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
using namespace std;

const int maxn = 10005;
const int maxm = 20000;
const int INF = 1e9;

char s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int rank[maxn], height[maxn];
void build_sa(int n, int m){
    int i, *x = t, *y = t2;
    for(i = 0; i < m; i++) c[i] = 0;
    for(i = 0; i < n; i++) c[x[i] = s[i]]++;
    for(i = 1; i < m; i++) c[i] += c[i - 1];
    for(i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(i = n - k; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
        for(i = 0; i < m; i++) c[i] = 0;
        for(i = 0; i < n; i++) c[x[y[i]]]++;
        for(i = 1; i < m; i++) c[i] += c[i - 1];
        for(i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(i = 1; i < n; i++)
        x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
}
void getHeight(){
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;
    for(i = 0; i < n; i++)
    {
        if(k) k--;
        j = sa[rank[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rank[i]] = k;
    }
}
int rmq[maxn][20];
void initRMQ(){
    for(int i = 1; i <= n; i++) rmq[i][0] = rmq[i][0] = height[i];
    for(int k = 1; (1 << k) <= n; k++)
    for(int i = 1; i + (1 << k) - 1 <= n; i++)
    {
        rmq[i][k]=min(rmq[i][k-1],rmq[i+(1<<(k-1))][k-1]);
    }
}
int lcp(int a,int b)
{
    a = rank[a], b = rank[b];
    if(a > b) swap(a, b);
    a++;
    int k = log(b - a + 1.0) / log(2.0);
    return min(rmq[a][k], rmq[b - (1 << k) + 1][k]);
}
int main()
{
    char ss[maxn];
    while(~scanf("%s", ss))
    {
        int len = strlen(ss);
        n = 0;
        for(int i = 0; i < len; i++) s[n++] = ss[i];
        s[n++] = '#';
        for(int i = len - 1; i >= 0; i--) s[n++] = ss[i];
        s[n] = 0;
        build_sa(n + 1, 256);
        getHeight();
        initRMQ();
        int st, ans = 1;
        for(int i = 0; i < len; i++)
        {
            int tmp = lcp(i, n - i - 1);
            if(tmp * 2 - 1 > ans)
            {
                ans = tmp * 2 - 1;
                st = i - tmp + 1;
            }
            tmp = lcp(i, n - i);
            if(tmp * 2 > ans)
            {
                ans = tmp * 2;
                st = i - tmp;
            }
        }
        if(ans == 1) printf("%c\n", ss[0]);
        else
        {
            for(int i = st; i < st + ans; i++) printf("%c", ss[i]);
            puts("");
        }
    }
    return 0;
}


你可能感兴趣的:(poj,后缀数组)