LightOJ 1258 Making Huge Palindromes (回文&KMP)
2015-08-14 10:33
281 查看
http://lightoj.com/volume_showproblem.php?problem=1258
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.
Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.
首先原串+翻转过来的串必然是一个回文串,但是二者在中间可以“融合”,而KMP算法恰好可以求出最大融合长度。
所以看翻转过来的串能匹配多少原串即可,答案就是len+(len-匹配个数)。
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 that 1 ≤ length(S) ≤ 106.
Output
For each case, print the case number and the length of the shortest palindrome you can make with S.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.首先原串+翻转过来的串必然是一个回文串,但是二者在中间可以“融合”,而KMP算法恰好可以求出最大融合长度。
所以看翻转过来的串能匹配多少原串即可,答案就是len+(len-匹配个数)。
#include<bits/stdc++.h> using namespace std; const int mx=100007; char t[mx],p[mx]; int f[mx],len; void getfail() { f[0]=f[1]=0; for(int i=1;p[i];i++) { int j=f[i]; while(j&&p[i]!=p[j]) j=f[j]; f[i+1]=(p[i]==p[j]?j+1:0); } } int findd() { getfail(); int j=0; for(int i=0;t[i];i++) { while(j&&p[j]!=t[i]) j=f[j]; if(p[j]==t[i]) ++j; } return (len<<1)-j; } int main() { //freopen("in.txt","r",stdin); int tt; scanf("%d",&tt); getchar(); for(int cas=1;cas<=tt;cas++) { gets(t); len=strlen(t); reverse_copy(t,t+len,p); p[len]=0; printf("Case %d: %d\n", cas, findd()); } return 0; }
相关文章推荐
- 在flash builder里全局搜索里如何“全字匹配”搜索
- js正则控制文本框只能输入数字以及小数
- 管式超滤系统:管式超滤膜技术在饮水净化中作用解析
- HttpClient Get和Post
- 别以为真懂Openstack系列:虚拟机创建的50个步骤和100个知识点
- 别以为真懂Openstack系列:虚拟机创建的50个步骤和100个知识点
- java: system.gc()和 finalize 的使用情景
- win10看视频总是弹出realtek高清晰音频管理器怎么办?
- 多线程六 经典线程同步之信号量Semaphore
- Gnome桌面的定制(修改面板 修改右键菜单) [初级]
- 进制转换
- Linux环境Nginx安装多版本PHP
- 微软中国裁员曝光:在CD结束后!薪酬不变!
- 网络篇----NSURLSession下载篇
- jquery在线手册
- HTML 提示 - 如何查看源代码
- 管式超滤系统:管式超滤膜的市场发展与应用领域
- Spring AOP的几种实现方式
- km算法
- Havok_2014-1-0_Pc_Xs_User_Guide(2.3.3-碰撞监听器)