poj_3974最长回文子序列基础题
来源:互联网 发布:大数据涂子沛概括 编辑:程序博客网 时间:2024/06/12 13:49
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 8166 Accepted: 3042
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
代码:
<span style="font-size:14px;">#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;char s[1000500];char temp[2000500];int p[2000500];int main(){ int t = 1; while(scanf("%s",&s)){ if(s[0] == 'E')break; int len = strlen(s); temp[0] = '$'; temp[len+1] = '$'; int num = 1; for(int i = 0; i < len; i++){ temp[num++] = '#'; temp[num++] = s[i]; } temp[num] = '#';//!!the total number of the string is num int maxid = 1; int maxright = 1; for(int i = 1; i <= 2*len+1; i++){ if(maxright < i){ maxright = i; maxid = i; } int backword = 2*maxid-i; if(p[backword]+i < maxright){ p[i] = p[backword]; } else{ while(temp[maxright+1] == temp[2*i-maxright-1]) maxright++; p[i] = maxright - i; maxid = i; } } int maxx = 0; int maxi; for(int i = 1; i <= num; i++) if(p[i] > maxx){ maxx = p[i]; maxi = i; } printf("Case %d: %d\n",t++,maxx); }}</span><span style="font-size:24px;color: blue;"></span>
0 0
- poj_3974最长回文子序列基础题
- poj_3974 Palindrome(最长回文子串)
- 最长回文子序列
- 最长回文子序列
- 最长回文子序列
- 最长回文子序列
- 最长回文子序列
- 最长回文子序列
- 最长回文子序列
- 【算法题】最长回文子序列
- hdu4745 最长回文子序列
- hdu1513 最长回文子序列
- LeetCode_5_LongestPalindromicSubstring(最长回文子序列)
- LPS最长回文子序列
- 最长回文子序列问题
- 最长回文子序列模板
- 最长公共回文子序列
- 最长回文串子序列
- Web上传文件的安全方式
- 三大 JS 框架的较量:Angular.js 与 React.js 与 Ember.js
- c++输入输出流操作笔记
- Codeforces#378 div.2 C. Epidemic in Monstropolis
- Android学习之路-------复习一下缓存和读取缓存,网络加载图片
- poj_3974最长回文子序列基础题
- 详细解释Basic disk和Dynamic disk
- 数据结构实验之数组二:稀疏矩阵
- Android颜色大全
- u-boot-2016.09移植(3)-u-boot-spl.bin
- PAT_乙级 1001 害死人不偿命的(3n+1)猜想
- Node.js学习笔记_模块调用
- UVA 10125 Sumsets .
- 第1章