首页 > 编程语言 > 详细

【后缀数组|最长回文子串】URAL-1297 Palindrome

时间:2015-03-15 23:03:13      阅读:450      评论:0      收藏:0      [点我收藏+]

1297.Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
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
output
ArozaupalanalapuazorA

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


题意: 输入一个字符串,求出它的子串中最长的回文串。
思路: 后缀数组的应用,可以将字符串倒着写一遍接到原串后面并在原串后面添加一个特殊字符,这样就可以枚举原串中的字符,它在新串中的后缀和它在反过来的字串中的后缀的LCP(最长公共前缀)即为回文子串的回文部分长度。对于LCP这个RMQ问题采取ST算法进行预处理即可。
这个时候分类讨论一下回文串长度为奇数和偶数这两种情况即可。
本做法时间复杂度由RMQ决定,为O(nlogn)。如果是Manacher算法将是O(n)。

[如果以上叙述没有看懂]
原串设为ABB,反过来后变成BBA,新串为”ABB#BBA0“,那么”B#BBA0”和”BA0”的LCP即为所求(B|B)
原串设为ABA,反过来后变成ABA,新串为”ABA#ABA0“,那么”BA#ABA0”和”BA0”的LCP即为所求(ABA)

代码如下:

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 2e3+5, M = 256;
char s[N];
int sa[N], t[N], t2[N], c[N], Rank[N], high[N];
int dp[N][32];

bool cmp(int *y, int i, int k)
{
    return y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k];
}

void da(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, p; k <= n; k<<=1, m=p) {
        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]] = cmp(y, i, k) ? p-1 : p++;
        }
        if(p >= n) break;
    }
}

void get_high(int n)
{
    int k = 0;
    for(int i = 1; i <= n; i++) Rank[sa[i]] = i;
    for(int i = 0; i < n; i++) {
        if(k) k--;
        int j = sa[Rank[i]-1];
        while(s[i+k] == s[j+k]) {
            k++;
        }
        high[Rank[i]] = k;
    }
}

void RMQ(int n)
{
    for(int i = 1; i <= n; i++) {
        dp[i][0] = high[i];
    }
    for(int j = 1; (1<<j) <= n; j++) {
        for(int i = 1; i+(1<<j)-1 <= n; i++) {
            dp[i][j] = min(dp[i][j-1], dp[i+(1<<(j-1))][j-1]);
        }
    }
}

int LCP(int a, int b)
{
    int L = Rank[a], R = Rank[b];
    if(L > R) swap(L, R);
    L++;
    int k = log(1.0 * (R-L+1)) / log(2.0);
    return min(dp[L][k], dp[R-(1<<k)+1][k]);
}

void solve(int n, int len)
{
    int ans = 1, idx = 0;
    for(int i = 0; i < len; i++) {
        int odd = LCP(i, n-i-1), even = LCP(i, n-i);
        if(2*odd-1 > ans) {
            ans = 2*odd - 1;
            idx = i - odd + 1;
        }
        if(2*even > ans) {
            ans = 2*even;
            idx = i - even;
        }
    }
    for(int i = idx; i < idx + ans; i++) {
        printf("%c", s[i]);
    }
    puts("");
}

int main()
{
#ifdef J_Sure
    //freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(~scanf("%s", s)) {
        int len = strlen(s);
        s[len] = ‘#‘;
        for(int i = 1; i <= len; i++) {
            s[len+i] = s[len-i];
        }
        int n = len<<1|1;
        s[n] = 0;
        da(n+1, M);
        get_high(n);
        RMQ(n);
        solve(n, len);
    }
    return 0;
}

【后缀数组|最长回文子串】URAL-1297 Palindrome

原文:http://blog.csdn.net/j_sure/article/details/44281243

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!