LightOJ 1258 - Making Huge Palindromes (KMP)
来源:互联网 发布:数据实时同步 编辑:程序博客网 时间:2024/04/30 01:57
1258 - Making Huge Palindromes
A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not.
Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.
For example, the string is 'bababa'. You can make many palindromes including
bababababab
babababab
bababab
Since we want a palindrome with minimum length, the solution is'bababab' cause its length is minimum.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing a string S. You can assume that1 ≤ length(S) ≤ 106.
Output
For each case, print the case number and the length of the shortest palindrome you can make withS.
Sample Input
Output for Sample Input
4
bababababa
pqrs
madamimadam
anncbaaababaaa
Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19
Note
Dataset is huge, use faster I/O methods.
题意:给出一个字符串,可以在字符串后面增加字符将其变为回文串。问最少增加几个字符原串能变为回文串,输出变为回文串后的字符串的最小长度。
题解:QAQ啊,又是不会。就是个很水的KMP啊。 将原串的后缀与逆序串的前缀相互匹配,求出他们的最长的相同锥长度,再用两个串的总长度(即两个原串的长度)减去最长相同缀长度就能得到所求的回文串长度。
例如:原串: ababb 逆序串:bbaba 将原串的后缀与逆序串的前缀匹配,等到最长相等缀为bb,长度为2,即所求回文串的长度为 2*5-2=8。
具体代码如下:
#include<cstdio>#include<cstring>#define max 1000010char s[max],str[max];int len,next[max];void getnext(){int i=0,j=-1;next[0]=-1;while(i<len){if(j==-1||str[i]==str[j])next[++i]=++j;elsej=next[j];}} void kmp(){int i=0,j=0;while(i<len){if(j==-1||s[i]==str[j]){i++;j++;}elsej=next[j];}printf("%d\n",2*len-j); }int main(){int t,k=1,i;scanf("%d",&t);while(t--){scanf("%s",s);len=strlen(s);for(i=0;i<len;++i)str[i]=s[len-1-i];printf("Case %d: ",k++);getnext();kmp();}return 0;}
- LightOJ 1258 Making Huge Palindromes (回文&KMP)
- LIGHTOJ 1258 – MAKING HUGE PALINDROMES 【KMP】
- LightOJ 1258 Making Huge Palindromes (回文&KMP)
- LightOJ 1258 - Making Huge Palindromes【kmp】
- LightOJ 1258 Making Huge Palindromes (回文&KMP)
- LightOJ 1258 - Making Huge Palindromes (KMP)
- LightOJ 1258 - Making Huge Palindromes
- Lightoj-1258 Making Huge Palindromes(kmp||回文串)
- lightoj 1258 - Making Huge Palindromes 【Manacher】
- LightOJ 1258 Making Huge Palindromes 【Manacher算法】
- light oj 1258 - Making Huge Palindromes(KMP)
- LightOJ 1258 Making Huge Palindromes(manacher算法)
- Light OJ 1258 - Making Huge Palindromes (KMP 或 Manacher)
- light OJ 1258 - Making Huge Palindromes【Manacher】
- light oj 1258 - Making Huge Palindromes (Manacher 变形)
- Light OJ 1258 Making Huge Palindromes 末尾添加最少字符变回文串
- LIGHT OJ 1258 - Making Huge Palindromes 【包含最后一位字符的最长回文串()】
- lightOJ 1258 【KMP变形题】
- SQL Server的CONVERT() 函数介绍
- DDL,DQL,DCL,DML
- 【bzoj1562】【noi2009】【变换序列】【匈牙利算法】
- mysql主从复制跳过错误
- javascript之DOM操作
- LightOJ 1258 - Making Huge Palindromes (KMP)
- 第九周项目1-猴子选大王
- 反复修改才能创造出优秀的代码
- android studio 中 .9.png的制作
- 数据结构实践——顺序串算法
- 正则表达式的贪婪匹配和非贪婪匹配
- 第八周上机实践项目5 - 计数的模式匹配
- Log4j详细使用教程
- linux下oracle创建用户步骤