poj 3974
来源:互联网 发布:优先次序矩阵 编辑:程序博客网 时间:2024/06/08 03:06
Manacher算法, 用来解决 最长回文子串 一类问题,俗称马拉车算法,时间复杂度
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>const int maxl = 1000005;const char end[5] = "END";#define clear(arr) memset(arr,0,sizeof(arr))char ch[maxl], s[maxl<<1];int sl, par[maxl<<1];void init(){ int l = strlen(ch); clear(s), clear(par); for(int i = 0; i < l; i++) s[i<<1|1] = ch[i], s[i<<1] = 255; sl = l<<1|1, s[sl-1] = 255;}int solve(){ int ret = 0; for(int i = 1, id = 0, mx = 0; i < sl-1; i++) { par[i] = (i < mx)?(std::min(mx-i,par[id*2-i])):1;#define Check(t) ((t)-par[t] >= 0 && (t)+par[t] < sl) while(Check(i) && s[i-par[i]] == s[i+par[i]]) par[i]++; if(i + par[i] > mx) id = i, mx = i + par[i]; ret = std::max(par[i] - 1, ret); } return ret;}int main(){ int T = 0;#ifndef ONLINE_JUDGE freopen("poj3974.in","r",stdin); freopen("poj3974.out","w",stdout);#endif while(scanf("%s",ch) != EOF) { if(!strcmp(ch, end)) break; printf("Case %d: ",++T); init(), printf("%d\n",solve()); }#ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout);#endif return 0;}
0 0
- POJ 3974
- poj 3974
- poj 3974
- poj 3974
- POJ 3974
- poj 3974 Palindrome
- poj 3974 Palindrome
- poj 3974 Palindrome
- poj 3974 HDU 3068
- poj 3974 Palindrome
- 【Manacher】 POJ 3974 Palindrome
- POJ 3974 Palindrome
- POJ 3974 Palindrome Manacher
- POJ 3974 Palindrome【Manacher】
- 【POJ 3974】 Palindrome
- POJ 3974 Palindrome
- poj 3974 Palindrome(manacher)
- POJ 3974 - Palindrome(manacher)
- Windows下的各种畸形目录和特殊文件
- Ubuntu 14.04(32位) Android Studio
- Android native Socket实现
- 黑马程序员_javaAPI中String类相关概述
- hihocoder Lucky Substrings
- poj 3974
- [华为机试练习题]60.水仙花数
- 高并发服务器处理
- apache的安装过程
- 关于Java中输入和输出最基础的东东
- [leetcode]longest consecutive sequence(java)
- yii框架学习——1. 安装
- 黑马程序员——01_比较器中比较结果顺序快速颠倒
- 初出茅庐