HDU 3068 最长回文 // Manacher
2015-10-27 00:05
288 查看
题目描述
HDU 3068 最长回文解题思路
题目大意: 输出最长回文子串的长度。Manacher算法传送门
参考代码
//********************************************** // Author: @xmzyt1996 // Date: 2015-10-26 // Name: HDU 3068.cpp //********************************************** #include <cstdio> #include <cmath> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <string> #include <bitset> #include <vector> #include <stack> #include <queue> #include <map> using namespace std; #define clr(a, b) memset(a, b, sizeof(a)) #define rep(i, a, b) for(int i = a; i < b; ++i) #define per(i, a, b) for(int i = a; i >= b; --i) #define pt(x) cout << #x << " = " << x << endl #define ps puts("debug~~") #define all(x) (x).begin(),(x).end() #define mp make_pair #define pb push_back typedef __int64 ll; typedef pair<int, int> pii; const double pi = acos(-1.0); const double eps = 1e-6; const int inf = 0x3f3f3f3f; const int MOD = 1e9+7; const int MAX_N = 110010; char s[MAX_N<<1], str[MAX_N]; int p[MAX_N<<1]; void Transform () { int k = 0; s[k++] = '$'; s[k++] = '#'; for (int i = 0; str[i]; ++i) { s[k++] = str[i]; s[k++] = '#'; } s[k] = 0; } int Manacher () { Transform(); int mx = 0, id = 0, ans = 0; clr (p, 0); for (int i = 1; s[i]; ++i) { p[i] = mx > i ? min(p[2*id-i], mx-i) : 1; while (s[i+p[i]] == s[i-p[i]]) p[i]++; if (i + p[i] > mx) { mx = i+p[i]; id = i; } ans = max(ans, p[i]-1); } return ans; } int main () { while (~scanf("%s", str)) printf("%d\n", Manacher()); return 0; }
相关文章推荐
- 在Ubuntu下开始搭建Python开发平台
- include属性选择
- 四大组件(四)—content provider
- [Leetcode]Binary Tree Zigzag Level Order Traversal
- 回顾 Steam Machines 与 SteamOS
- 回顾 Steam Machines 与 SteamOS
- Linux基础命令日常积累
- JSP实现的简单分页显示效果代码
- jsp实现针对excel及word文档的打印方法
- MongoDB正则表达式及应用
- Java用正则表达式如何读取网页内容
- 三种asp.net页面跳转的方法
- asp.net操作Word实现批量替换
- php+ajax实现无刷新的新闻留言系统
- php语言中使用json的技巧及json的实现代码详解
- php邮件发送的两种方式
- 免费 UGUI 插件 Inventory Master 背包系统使用方法
- 一个完整的php文件上传类实例讲解
- PHP的APC模块实现上传进度条
- php过滤所有的空白字符(空格、全角空格、换行等)