light OJ 1258 - Making Huge Palindromes【Manacher】
2015-10-26 17:54
696 查看
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.
Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.
PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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.#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int N = 1000005; char s , a[N*2]; int p[N*2]; void Manacher(char s[], int len) { int i,j,k = 0; a[k++] = '$'; a[k++] = '#'; for(i = 0; i < len; i++) { a[k++] = s[i]; a[k++] = '#'; } a[k] = 0; int mx=0,id=0; for(i = 0; i < k; i++) { p[i] = mx>i?min(p[2*id-i], mx-i):1; while(a[i+p[i]] == a[i-p[i]]) p[i]++; if(i+p[i] > mx) { mx = i+p[i]; id = i; } } } int main() { int t,ca = 1; scanf("%d",&t); while(t--) { scanf("%s", s); int len = strlen(s); Manacher(s, len); int ans = 0; int i,j; for(i = 0; i <= 2*len+1; i++) { if(i+p[i]==2*len+2) { ans = max(ans, p[i]-1); } } printf("Case %d: %d\n", ca++, len+len-ans); } return 0; }
相关文章推荐
- Codeforces Round #327 (Div. 2) B. Rebranding 模拟
- PHP垃圾回收机制
- 使用js实现前端缓存
- C++:对象的初始化和构造函数
- 关于ListView和GridView的应用
- 设计模式 --> (2)单例模式
- 夺命雷公狗jquery---22-bind为jquery对象绑定多个相关事件
- LintCode : 有效回文串
- jsp中getParameter() 和 getAttribute()
- MediaCodec : H264硬解码核心代码总结
- 【JAVA核心技术卷一】Interface 接口
- JAVA WEB 导出 Word
- Yii2 自定义过滤器
- 【JAVA核心技术卷一】Inheritance 继承
- 1022. D进制的A+B (20)
- 回文子串【Manacher】算法时间复杂度O(n)
- Java 四大域对象总结
- Java WebService入门实例
- redis基本操作
- 异步消息的传递-回调机制