Hzu_5.D.Palindrome. Again Palindrome

http://vjudge.net  hzu5  D题

1085. Meeting

Time limit: 2.0second

Memory limit: 64 MB

KfA 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). If S1 = 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 Latinletters). It’s guaranteed that the length of S1 doesn’t exceed 10000 symbols.

Output

S1S2.

Sample

input

output

No

NoN

OnLine

OnLineniLnO

AbabaAab

AbabaAababA

题目大意:

定义单词是非空的符号序列a1a2…an。回文词是一个单词a1a2…an,从左往右阅读和从右往左阅读都是一样的( a1a2…an= anan−1… a1)。定义S1= a1a2…an,S2 = b1b2…bm,则S1S2= a1a2…anb1b2…bm。程序输入一个单词S1。你要找到一个非空的单词S2,令S1S2是一个长度最小的回文词。

输入:

第一行输入一个S1(只由拉丁字母组成)。保证S1不超过10000个符号。

输出:

S1S2

解题思路:

找回文词,根据S1找到非空并且最短的S2,使S1S2为回文词

将输入的S1反调储存在S2中,再将S1和S2进行错位比较,找到重复的S1尾和S2头重复的部分,输出S1,S2­重复部分之后的字符串即可

相应代码:

#include

#include

usingnamespacestd;

constintmaxn = 10000 + 5;

charstr[maxn];

charback[maxn];

intmain()

{

cin >> str;

intlen =strlen(str);

for(inti = 0; i < len; ++i)

back[i] = str[len - i - 1];

if(len ==1)

cout << str << str<< endl;

else

{

for(intl = len - 1; l > 0; --l)

{

boolok =true;

for(inti = 0; i < l; ++i)

if(str[len - l + i] != back[i])

{

ok =false;

break;

}

if(ok)

{

cout << str <

break;

}

}

}

return0;

}

你可能感兴趣的:(Hzu_5.D.Palindrome. Again Palindrome)