LightOJ 1258 (Manacher 或者KMP都行)
2015-10-24 19:34
471 查看
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96545#problem/D
Description:
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:
4
bababababa
pqrs
madamimadam
anncbaaababaaa
Sample Output:
Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19
题意:有一个字符串,不一定是回文串,现在问最少要添多少字符才能使该字符串变为回文串,输出最终回文串的最小长度。看完就觉得是Manacher,但是怎么改都wa了。。。后来发现其实只要找到以字符串结尾结束的最长回文串的长度就好了。。。。但是有不少人用的Kmp(个人觉得Manacher更好理解)。
Description:
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:
4
bababababa
pqrs
madamimadam
anncbaaababaaa
Sample Output:
Case 1: 11
Case 2: 7
Case 3: 11
Case 4: 19
题意:有一个字符串,不一定是回文串,现在问最少要添多少字符才能使该字符串变为回文串,输出最终回文串的最小长度。看完就觉得是Manacher,但是怎么改都wa了。。。后来发现其实只要找到以字符串结尾结束的最长回文串的长度就好了。。。。但是有不少人用的Kmp(个人觉得Manacher更好理解)。
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<queue> #include<algorithm> using namespace std; const int N=1e6+10; const int MOD=1e9+7; const int INF=0x3f3f3f3f; char a , s[2*N]; int k, r[2*N]; int Palindrome() { int Max = 0, id = 0, i; memset(r, 0, sizeof(r)); for (i = 2; i < k; i++) { r[i] = 1; if (r[id]+id > i) r[i] = min(r[2*id-i], r[id]+id-i); while (s[i-r[i]] == s[i+r[i]]) r[i]++; if (r[i]+i > r[id]+id) id = i; if (r[i]-1 > Max && r[i]+i == k) Max = r[i]-1; } return Max; } int main () { int T, i, len, num = 0, Max; scanf("%d", &T); while (T--) { scanf("%s", a); len = strlen(a); k = 2; num++; memset(s, 0, sizeof(s)); s[0] = '$'; s[1] = '#'; for (i = 0; a[i] != '\0'; i++) { s[k++] = a[i]; s[k++] = '#'; } Max = Palindrome(); printf("Case %d: %d\n", num, len+(len-Max)); } return 0; }
相关文章推荐
- 聪明的投资者第一章
- lightoj--1005--Rooks(组合数)
- javascript中作用域和作用域链的例子
- lightoj--1005--Rooks(组合数)
- 大道至简读后感
- Linux的环境变量设置
- C++迭代器 iterator
- 2015-2016 ACM-ICPC, NEERC, Moscow Subregional Contest C题:Colder-Hotter [三分/交互题]
- kill 、killall、pkill、xkill pgrep ps 命令解释 2015年10月24日
- liunx删除文件后磁盘未减小
- 安川交流伺服电机的驱动
- iOS 设置代理过程
- JAVA课程作业04
- 一个招标书文件的需求分析
- 关 于 环 境 变 量 的 类 习 题
- HDU 4340 Capturing a country(树形DP)
- 第四次作业
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十六)
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十六)
- (NO.00001)iOS游戏SpeedBoy Lite成形记(十六)