您的位置:首页 > 其它

URAL 1297 Palindrome (最长回文子串)

2014-10-18 09:48 501 查看

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

inputoutput
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA

ArozaupalanalapuazorA

分析:将原串反向加到后面,并用特殊字符分隔,做后缀数组即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <string>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 5005;

//以下是倍增法求后缀数组
int wa[maxn], wb[maxn], wv[maxn], ws[maxn];
int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; }
void da(int *r, int *sa, 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 <<= 1, 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++;
}
}

//以下是求解height 数组
int height[maxn], Rank[maxn];
void calheight(int *r, int *sa, int n) {
int i, j, k = 0;
for(i = 1; i <= n; i++) Rank[sa[i]] = i;
for(i = 0; i < n; height[Rank[i++]] = k)
for(k ? k-- : 0, j = sa[Rank[i] - 1];
r[i + k] == r[j + k]; k++) ;
}

char buf[maxn];
int str[maxn], sa[maxn], len, n;
int minv[maxn][20];

void init_RMQ() {
for(int i = 0; i <= len; i++) minv[i][0] = height[i];
for(int j = 1; (1 << j) <= len + 1; j++) {
for(int i = 0; i + (1 << j) - 1 <= len; i++) {
minv[i][j] = min(minv[i][j - 1], minv[i + (1 << (j - 1))][j - 1]);
}
}
}

int query(int ql, int qr) {
if(ql > qr) swap(ql, qr);
ql++;
int k = 0;
while((1 << (k + 1)) <= qr - ql + 1) k++;
return min(minv[ql][k], minv[qr - (1 << k) + 1][k]);
}

int main() {
while(scanf("%s", buf) != EOF) {
n = len = strlen(buf);
for(int i=0;i<len;i++) str[i] = buf[i];
str[len] = '*';
for(int i=n-1;i>=0;i--) str[++len] = buf[i];
str[++len] = 0;
da(str, sa, len + 1, 200);
calheight(str, sa, len);
init_RMQ();
int anspos=0, anslen=1;
for(int i=0;i<n;i++)
{
int a = query(Rank[i],Rank[len-i-1]);
int b = a*2-1;
if(b>anslen)
{
anslen = b;
anspos = i-(a-1);
}
a = query(Rank[i],Rank[len-i]);
b = a*2;
if(b>anslen)
{
anslen = b;
anspos = i-a;
}
}
for(int i=anspos;i<anspos+anslen;i++) putchar(buf[i]);
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: