lightoj 1258 - Making Huge Palindromes 【Manacher】
来源:互联网 发布:网络平台招商加盟 编辑:程序博客网 时间:2024/05/16 06:24
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.
#include <cstdio>#include <cstring>#include <algorithm>#define MAXN 1001000using namespace std;char s[MAXN], str[MAXN*2];int p[MAXN*2];int ans, Mlen;int l;void Manacher(char *T){ int len = strlen(T); l = 0; str[l++] = '@'; str[l++] = '#'; for(int i = 0; i < len; i++) { str[l++] = T[i]; str[l++] = '#'; } str[l] = 0; int mx = 0, id = 0; ans = Mlen = 0; for(int i = 0; i < l; i++) { if(mx > i) p[i] = min(p[2*id-i], mx-i); else p[i] = 1; while(str[i+p[i]] == str[i-p[i]]) p[i]++; if(i + p[i] > mx) { mx = i + p[i]; id = i; } ans = max(ans, p[i]-1); if(p[i] - 1 + i == l - 1)//包含最后一个字符 更新 Mlen = max(Mlen, p[i]-1); }}int kcase = 1;void solve(char *s){ Manacher(s); int len = strlen(s); if(ans == len) printf("Case %d: %d\n", kcase++, ans); else printf("Case %d: %d\n", kcase++, len - Mlen + len);}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%s", s); solve(s); } return 0;}
- lightoj 1258 - Making Huge Palindromes 【Manacher】
- LightOJ 1258 Making Huge Palindromes 【Manacher算法】
- LightOJ 1258 Making Huge Palindromes(manacher算法)
- LightOJ 1258 - Making Huge Palindromes
- light OJ 1258 - Making Huge Palindromes【Manacher】
- 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)
- Light OJ 1258 - Making Huge Palindromes (KMP 或 Manacher)
- light oj 1258 - Making Huge Palindromes (Manacher 变形)
- Lightoj-1258 Making Huge Palindromes(kmp||回文串)
- light oj 1258 - Making Huge Palindromes(KMP)
- Light OJ 1258 Making Huge Palindromes 末尾添加最少字符变回文串
- LIGHT OJ 1258 - Making Huge Palindromes 【包含最后一位字符的最长回文串()】
- lightoj 1258(manacher)
- Android:View绘制流程
- boost asio client
- 分享几个程序员应该知道的网站和技术博客
- javascript基本语法 5 引用类型-----去掉数组中的重复项
- 关于UIScreen的applicationFrame和bounds方法区别
- lightoj 1258 - Making Huge Palindromes 【Manacher】
- 在grub中添加win7(以及从win7来的win10)的启动项
- 如何清除windows入侵的记录
- strcpy函数的实现
- 硬件-CPU3
- 理解OAuth 2.0
- Java String中若干问题
- 集合中的其他类
- 使用Ruby On Rails15分钟打造一个博客系统