poj3974_Palindrome_
来源:互联网 发布:python单行注释 编辑:程序博客网 时间:2024/06/05 00:13
Palindrome
Time Limit: 15000MS Memory Limit: 65536KTotal Submissions: 8974 Accepted: 3376
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
题意:求回文串。
解:Manacher的O(n)解法。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;const int MANX = 2e6+100;char str0[MANX],str[MANX<<1];int p[MANX<<1],len;void start(){ str[0]=-1,str[1]=-2; len=strlen(str0); for(int i=0;i<len;i++) { str[i*2+2]=str0[i]; str[i*2+3]=-2; } str[len*2+2]=-6;}void solve(){ memset(p,0,sizeof(p)); int mx=0,id,ans=1; for(int i=1;i<len*2+2;++i) { if(mx>i)p[i]=min(p[2*id-i],mx-i); else p[i]=1; for(;str[i-p[i]]==str[i+p[i]];p[i]++); if(p[i]+i>mx)mx=p[i]+i,id=i; if(p[i]>ans)ans=p[i]; } printf("%d\n",--ans);}int main(){ int t=0; while(~scanf("%s",str0)){ if(str0[0]=='E') break; cout<<"Case "<<++t<<": "; start(); solve(); } return 0;}
1 0
- poj3974_Palindrome_
- 浅谈ES6原生Promise
- 【object detection】YOLO详解
- sql语句 判断表数据是否存在另一个表中
- C#的yield语法糖研究(转载)
- UVa1586 Molar Mass
- poj3974_Palindrome_
- idea选择自己的包创建SSH项目
- Linux内核中断系列之中断的下半部(八)
- DAY6作业-百度登录框
- iOS获取类名NSStringFromClass 和 获取类NSClassFromString
- Lru cache算法以及他的锁的问题
- OpenStack安装与配置
- 动态规划为什么就是经典?
- BubbleSeekBar开源框架在andorid7.0无法适配的解决方案