PKU 3974 - Palindrome (最长回文字串 DP)
来源:互联网 发布:log4j打印sql日志 编辑:程序博客网 时间:2024/05/23 18:32
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 3777 Accepted: 1363
Description
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).
Output
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input
abcbabcbabcbaabacacbaaaabEND
Sample Output
Case 1: 13Case 2: 6
Source
Seventh ACM Egyptian National Programming Contest
求最长回文字串
o(n)
#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxn = 2000000 + 20;char s[maxn];int p[maxn];char str[maxn];int n;void kp() { int mx = 0; int id; for(int i=1; i<n; 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; } }}void pre() { n = strlen(s); str[0] = '$'; str[1] = '#'; for(int i=0; i<n; i++) { str[i*2+2] = s[i]; str[i*2+3] = '#'; } n = n*2 + 2; str[n] = 0;}int getAns() { int ans = 0; for(int i=0; i<n; i++) { ans = max(ans, p[i]); } return ans-1;}int main() { int kase = 1; while(scanf("%s", s) != EOF && strcmp(s, "END")) { pre(); kp(); int ans = getAns(); printf("Case %d: %d\n", kase++, ans); } return 0;}
0 0
- PKU 3974 - Palindrome (最长回文字串 DP)
- hdu-4632-Palindrome subsequence DP&回文字串
- 最长回文串dp(uva11151 - Longest Palindrome)
- uval1297 Palindrome 后缀数组求最长回文字串,lcp,rmq
- poj 3974 Palindrome 最长回文
- POJ 3974 最长回文字串(manacher算法)
- Longest Palindromic Substring(最长回文字串)
- 后缀数组(最长回文字串)ural1297
- 最长回文字串(mancher算法)
- 最长回文字串
- 求解最长回文字串
- 最长回文字串
- 最长回文字串
- 最长回文字串
- 最长回文字串
- 最长回文字串
- hihocoder1032:最长回文字串
- 最长回文字串 lintcode
- stm32 DAC+DMA 输出100K正弦波杠杠的!!!
- 1016: [JSOI2008]最小生成树计数
- pthread
- mapreduce任务使用leveldb多线程问题
- C++ 对象模型 学习笔记(2)
- PKU 3974 - Palindrome (最长回文字串 DP)
- 2048游戏C语言代码
- BS几种技术及它们之间关系
- LWIP分析与socket编程方法
- Cocos2d-x3.1下实现类似iOS页面滑动指示圆点
- IO流--IO异常的处理方式
- 白醋美容妙妙妙!
- VirtualBox无法打开,COM对象创建失败
- 异常知识点小结