Ural 1354. Palindrome. Again Palindrome KMP的应用
2013-07-27 12:15
330 查看
1354. Palindrome. Again Palindrome
Time limit: 1.0 secondMemory 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’texceed 10000 symbols.
Output
S1S2.Samples
input | output |
---|---|
No | NoN |
OnLine | OnLineniLnO |
AbabaAab | AbabaAababA |
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; }
相关文章推荐
- URAL 1354 Palindrome. Again Palindrome (回文 + KMP)
- URAL 1354. Palindrome. Again Palindrome
- URAL 1684. Jack's Last Word ( KMP next函数应用 )
- hdu 1867(A + B for you again) KMP的应用 /hdu 2594(Simpsons’ Hidden Talents) KMP
- 1354. Palindrome. Again Palindrome
- 【manacher变形】Timus OJ 1354. Palindrome. Again Palindrome 包尾部最长回文串
- HDU 1867 A + B for you again 数据结构+KMP简单应用
- HDU 1867 A + B for you again (KMP应用)
- 1354. Palindrome. Again Palindrome
- 关于 KMP next 数组的应用
- 数据结构实验之串三:KMP应用
- 数据结构--KMP简单应用
- [NOI2014][bzoj3670] 动物园 [kmp+next数组应用]
- poj--3080Blue Jeans KMP的简单应用
- URAL 1297 Palindrome(SA 求最长回文子串)
- 数据结构实验之串三:KMP应用
- 数据结构实验之串一:KMP简单应用
- 数据结构实验之串一:KMP简单应用 (sdut oj2772)
- URAL 1297 Palindrome
- 数据结构实验之串一:KMP简单应用