您的位置:首页 > 其它

URAL 1297 Palindrome

2013-12-21 16:53 363 查看
manacher....

Palindrome

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
[Submit]   [Go
Back]   [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
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA

output
ArozaupalanalapuazorA

Source

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

[Submit]   [Go
Back]   [Status]  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

char str[1100],ans[3300];
int p[3300],pos,how;

void pre()
{
int tot=1;
memset(ans,0,sizeof(ans));
ans[0]='$';
int len=strlen(str);
for(int i=0;i<len;i++)
{
ans[tot]='#';tot++;
ans[tot]=str[i];tot++;
}
ans[tot]='#';
}

void manacher()
{
pos=-1;how=0;
memset(p,0,sizeof(p));
int len=strlen(ans);
int mid=-1,mx=-1;
for(int i=0;i<len;i++)
{
int j=-1;
if(i<mx)
{
j=2*mid-i;
p[i]=min(p[j],mx-i);
}
else p[i]=1;

while(i+p[i]<len&&ans[i+p[i]]==ans[i-p[i]])
{
p[i]++;
}

if(p[i]+i>mx)
{
mx=p[i]+i; mid=i;
}
if(p[i]>how)
{
how=p[i]; pos=i;
}
}
}

int main()
{
while(scanf("%s",str)!=EOF)
{
pre();
manacher();
how--;
for(int i=pos-how;i<=pos+how;i++)
{
if(ans[i]!='#') putchar(ans[i]);
}
putchar(10);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  manacher URAL 字符串