POJ 3974-Palindrome(Manacher算法)
2015-08-03 12:31
363 查看
题目地址:POJ 3974
题意:求最长的回文串。
思路:同样是用Mancher算法在O(n)的时间内解决(我其实是来练练板子的
题意:求最长的回文串。
思路:同样是用Mancher算法在O(n)的时间内解决(我其实是来练练板子的
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const double pi= acos(-1.0); const double esp=1e-6; const int maxn=1000010; char str[maxn]; char s[maxn*2]; int p[maxn*2]; int len; void Manacher() { int i; s[0]='$'; s[1]='#'; for(i=0;i<len; i++) { s[i*2+2]=str[i]; s[i*2+3]='#'; } len=i*2+2; s[len]='\0'; int MaxL,id=0; MaxL=0; memset(p,0,sizeof(p)); for(int i=0; i<len; i++) { if(p[id]+id>i) p[i]=min(p[2*id-i],p[id]+id-i); else p[i]=1; while(s[i+p[i]]==s[i-p[i]]) p[i]++; if(p[i]+i>p[id]+id) { id=i; } if(p[i]>MaxL) MaxL=p[i]; } printf("%d\n",MaxL-1); } int main() { int icase=1; while(~scanf("%s",&str)) { if(strcmp(str,"END")==0) break; len=strlen(str); printf("Case %d: ",icase++); Manacher(); } return 0; }
相关文章推荐
- HDU 3476 Cyclic Nacklice
- lua 栈,checkElement支持删除元素操作
- 用Python实现最速下降法求极值
- 微信开发总结 一
- SNMP++
- ios使用TextField输入框
- android事件传递流程 onTouchEvent onInterceptTouchEvent()
- UVA10177数出二维三维四维中的正方和矩方数
- hdu 1283 最简单的计算机
- 在Mac OS X 上 删除项目中所有.svn 文件
- hkpDynamics->hkpRigidBody
- ExtJs store.sync() 出现indexof not defined错误
- maven的常用技巧
- order 根据不同条件排序
- ./configure,make,make install的作用
- Why Coding Like this -------Filter函数揭秘
- Why Coding Like this -------map函数揭秘
- lua 高效hashmap (没调试)
- Exchange2013之HTTP重定向
- Android IOS WebRTC 音视频开发总结(三六)-- easyRTC介绍