您的位置:首页 > 大数据 > 人工智能

Ural 1354. Palindrome. Again Palindrome KMP的应用

2013-07-27 12:15 330 查看


1354. Palindrome. Again Palindrome

Time limit: 1.0 second

Memory limit: 64 MB

A word is the nonempty sequence of symbols a1a2…an.
A palindrome is the word a1a2…an that
is read from the left to the right and from the right to the left the same way (a1a2…an = anan−1…a1).
IfS1 = a1a2…an and S2 = b1b2…bm,
then S1S2 = a1a2…anb1b2…bm.
The input contains some word S1. You are to find a nonempty word S2 of the minimal
length that S1S2 is a palindrome.

Input

The first input line contains S1 (it may consist only of the Latin letters). It’s guaranteed that the length of S1 doesn’t
exceed 10000 symbols.

Output

S1S2.

Samples

inputoutput
No

NoN

OnLine

OnLineniLnO

AbabaAab

AbabaAababA

Problem Author: Denis Nazarov

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: