ural1297. Palindrome

1297. 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

题目大意

给定一个字符串,求最长回文子串。

题解

将所给串反向与原串相连,中间以特殊字符隔开。求以某个字符为中心的最长回文串就转化为原串某个后缀和反转串某个后缀的最长前缀长度,构建出height,上RMQ,注意下细节以及有若干个相同长度的输出第一个出现的即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#define min(x, y) ((x) < (y) ? (x) : (y))
using namespace std;

const int N = 2000 + 10;
char s[N];
int n, df, a[N], sa[2][N], rk[2][N], ht[N], v[N];
int p, q, k;
int st[N][20], mn[N];
int ans, flg, pos;

void init(){
    scanf("%s", s+1);
    int tot;
    tot = strlen(s+1);
    for(int i = 1; i <= tot; i++) a[i] = (char) s[i];
    n = tot;
    a[++n] = (char) '$';
    df = n;
    for(int i = tot; i >= 1; i--) a[++n] = (char) s[i];
}

void calsa(int *sa1, int *rk1, int *sa2, int *rk2){
    for(int i = 1; i <= n; i++) v[rk1[sa1[i]]] = i;
    for(int i = n; i >= 1; i--) if(sa1[i] > k) sa2[v[rk1[sa1[i]-k]]--] = sa1[i] - k;
    for(int i = n-k+1; i <= n; i++) sa2[v[rk1[i]]--] = i;
    for(int i = 1; i <= n; i++) rk2[sa2[i]] = rk2[sa2[i-1]] +
        (rk1[sa2[i-1]] != rk1[sa2[i]] || rk1[sa2[i-1]+k] != rk1[sa2[i]+k]);
}

void calht(){
    int k = 0;
    for(int i = 1; i <= n; i++){
        if(k) k--;
        int j = sa[p][rk[p][i]-1];
        while(a[i+k] == a[j+k]) k++;
        ht[rk[p][i]] = k;
    }
}

void calst(){
    mn[0] = -1;
    for(int i = 1; i <= n; i++){
        mn[i] = (i & i-1) ? mn[i-1] : mn[i-1] + 1;
        st[i][0] = ht[i];
    }
    for(int j = 1; j <= mn[n]; j++)
        for(int i = 1; i + (1<<j) - 1 <= n; i++)
         st[i][j] = min(st[i][j-1], st[i+(1<<j-1)][j-1]);
}

int calmn(int x, int y){
    if(x > y) swap(x, y);
    x++;
    int k = mn[y-x+1];
    return min(st[x][k], st[y-(1<<k)+1][k]);
}

void work(){
    p = 0, q = 1, k = 1;
    for(int i = 1; i <= n; i++) v[a[i]]++;
    for(int i = 1; i <= 130; i++) v[i] += v[i-1];
    for(int i = 1; i <= n; i++) sa[p][v[a[i]]--] = i;
    for(int i = 1; i <= n; i++) rk[p][sa[p][i]] = rk[p][sa[p][i-1]] + (a[sa[p][i]] != a[sa[p][i-1]]);
    memset(v, 0, sizeof(v));
    while(k < n){
        calsa(sa[p], rk[p], sa[q], rk[q]);
        p ^= 1, q ^= 1, k <<= 1;
    }
    calht();
    calst();
    for(int i = 1; i < df; i++){
        int tmp = calmn(rk[p][i], rk[p][n-i+1]);
        if(tmp*2-1 > ans*2-flg) ans = tmp, flg = 1, pos = i;
        tmp = calmn(rk[p][i+1], rk[p][n-i+1]);
        if(tmp*2 > ans*2-flg) ans = tmp, flg = 0, pos = i;
    }
    if(flg)
        for(int i = pos-ans+1; i <= pos+ans-1; i++) printf("%c", a[i]);
    else
        for(int i = pos-ans+1; i <= pos+ans; i++) printf("%c", a[i]);
    puts("");
}

int main(){
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值