1354. Palindrome. Again Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
A
word is the nonempty sequence of symbols
a
1
a
2…
an. A
palindrome is the word
a
1
a
2…
an that is read from the left to the right and from the right to the left the same way (
a
1
a
2…
an =
ana
n−1…
a
1). If
S
1 =
a
1
a
2…
an and
S
2 =
b
1
b
2…
bm, then
S
1
S
2 =
a
1
a
2…
anb
1
b
2…
bm. The input contains some word
S
1. You are to find a nonempty word
S
2 of the minimal length that
S
1
S
2 is a palindrome.
Input
The first input line contains
S
1 (it may consist only of the Latin letters). It’s guaranteed that the length of
S
1 doesn’t exceed 10000 symbols.
Output
S
1
S
2.
Samples
input | output |
---|---|
No | NoN |
OnLine | OnLineniLnO |
AbabaAab | AbabaAababA |
Problem Author: Denis Nazarov
Problem Source: USU Junior Championship March'2005
Problem Source: USU Junior Championship March'2005
构造回文串
#include <cstdio>
#include <cstdlib>
#include <cstring>
int i,j,l,next[10010];
char s[10010];
int main()
{
scanf("%s",&s[1]);
printf("%s",&s[1]);
l=strlen(s+1);
next[l]=l+1;
i=l;
j=l+1;
while (i > 1)
if ((j > l) || (s[i] == s[j]))
{
--i;
--j;
if (s[i] == s[j])
next[i]=next[j];
else next[i]=j;
}
else j=next[j];
i=2;
j=l;
while (i <= l)
if ((j > l) || (s[i] == s[j]))
++i,--j;
else j=next[j];
for (; j!=0; --j)
printf("%c",s[j]);
printf("\n");
return 0;
}