poj3974:Palindrome(manacher模板)
来源:互联网 发布:淘宝代刷自动返款系统 编辑:程序博客网 时间:2024/05/22 13:46
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.
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
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
题意:
给一个字符串,求最长回文串。
题解:
Manacher模板,时间复杂度O(n)。
Manacher算法流程
代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>#include<cmath>using namespace std;const int Maxn=1e6+50;char ch[Maxn*2],s[Maxn];int len,tt,tot,pos,id,ans,R[Maxn*2];inline void manacher(){ id=0,pos=0,ans=0; int x=0; for(int i=1;i<=tot;i++) { if(pos>i)x=min(pos-i,R[2*id-i]); else x=1; while(ch[i+x]==ch[i-x])++x; if(i+x>pos)pos=i+x,id=i; R[i]=x; ans=max(R[i],ans); }}int main(){ while(scanf("%s",s+1),s[1]!='E') { tt++; len=strlen(s+1); tot=0; ch[++tot]='!'; ch[++tot]='#'; for(int i=1;i<=len;i++) { ch[++tot]=s[i]; ch[++tot]='#'; } ch[++tot]='?'; manacher(); printf("Case %d: %d\n",tt,ans-1); }}
- poj3974:Palindrome(manacher模板)
- poj3974 Palindrome(manacher)
- POJ3974 Palindrome(Manacher算法)
- 【POJ3974】Palindrome Manacher、模板题 裸题
- Palindrome(poj3974)(manacher算法)
- POJ3974 Palindrome (manacher算法)
- 【POJ3974】【Palindrome】【Manacher裸题】
- POJ3974 Palindrome(Manacher)
- [学习][poj3974]manacher Palindrome
- poj3974 Palindrome(manacher)
- Manacher模板 Poj3974
- poj3974 manacher模板
- poj3974 Palindrome,回文串,Manacher
- hdu3068 最长回文 poj3974 Palindrome(Manacher算法)
- poj3974(manacher)
- POJ3974 Palindrome 解题报告【字符串】【Manacher】
- POJ3974 Palindrome
- poj3974 Palindrome
- csu1798: 小Z的城市 线段树+ST表
- android之ListView重写item里面的事件
- shell中的变量赋值
- android网络配置
- 改不少次,全是小错误
- poj3974:Palindrome(manacher模板)
- make modules_install指定ko安装路径
- C#如何查找指定目录下文件示例
- cs231n学习心得
- 十、ngMockE2E模块,后台数据模拟
- Java.面向对象.02.继承、向上造型等
- linux初级学习之文件权限 第六单元 学习笔记
- C++ primer 5 3.6节练习:遍历二维数组的各种形式
- android之多进程下Application.getSharedPreferences的取值