POJ 3974 Palindrome 求最长回文子串 Manacher
2015-11-04 13:17
483 查看
纯模板题,用来练手,模板记错贡献一次wa。
[Submit] [Go
Back] [Status] [Discuss]
Home Page
Go
Back
To top
ACode:
Palindrome
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 Source Seventh ACM Egyptian National Programming Contest |
Back] [Status] [Discuss]
Home Page
Go
Back
To top
ACode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 1000005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
char str[maxn];
char ma[maxn<<1];
int mp[maxn<<1];
int cnt=1,ans;
void Manacher(){
int l=0;ma[l++]='$';ma[l++]='#';
int m=strlen(str);ans=-INF;
FOR(i,0,m-1){
ma[l++]=str[i];
ma[l++]='#';
}
ma[l]=0;
int mx=0,id=0;
FOR(i,0,l-1){
mp[i]=mx>i?min(mp[2*id-i],mx-i):1;
while(ma[i+mp[i]]==ma[i-mp[i]])mp[i]++;
if(mp[i]+i>mx){
mx=mp[i]+i;
id=i;
}
ans=max(ans,mp[i]);
}
printf("Case %d: %d\n",cnt++,ans-1);
}
int main(){
while(rds(str)&&strcmp(str,"END"))
Manacher();
return 0;
}
/*
abcbabcbabcba abacacbaaaab END
*/
相关文章推荐
- Newtonsoft.Json 自定义 解析协议
- JS正则表达式验证
- 切图psd to html页面制作技术学习与总结
- 细碎之java
- 再别康桥
- eclipse自动补全
- glfw3的使用
- android stdio 启动报错
- Android Handler的使用一
- DDNS技术
- 从”JAVA“而终 11:Enum的用法
- convertView&setTag方法的一点理解
- Android项目重构之路:架构篇
- Cousera 502 bad getway 解决方法
- Mysql命令大全
- hdoj3979Monster【贪心】
- 百度移动搜索系统将进行升级
- iOS开发笔记--UIView中的坐标转换
- Shape使用和button 的点击效果
- extjs 里执行多行选中,和单行选中的操作