Ural 1354. Palindrome. Again Palindrome KMP的应用

1354. Palindrome. Again Palindrome

Time limit: 1.0 second
Memory limit: 64 MB
word is the nonempty sequence of symbols  a 1 a 2an. A  palindrome is the word  a 1 a 2an that is read from the left to the right and from the right to the left the same way ( a 1 a 2an =  ana n−1a 1). If S 1 =  a 1 a 2an and  S 2 =  b 1 b 2bm, then  S 1 S 2 =  a 1 a 2anb 1 b 2bm. 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
构造回文串
#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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值