HDU 4763 Theme Section(KMP)
来源:互联网 发布:影视大全为什么没网络 编辑:程序博客网 时间:2024/06/05 22:39
题目链接:点击打开链接
题意:给你一个字符串s, 要你求是否存在一个子串, 在s的前中后各出现一次。 求最大可能的子串长度。
思路:KMP变形, 我们首先要了解KMP的运行机制。 核心是失配函数f, f[i]表示在i点失配之后返回到f[i]这个点, 且能保证f[i]之前的部分和模板串匹配。 这样的话, 我们就可以利用这个特点来处理该题了。 我们假设在最后一个字符处失配, 那么它将返回f[len], 那么在f[len]之前的部分肯定是和s串的后边f[len]个字符相匹配的。 而前f[len]个字符是不用考虑的, 那么我们需要考虑的就是中间的部分是否存在一个f[len]长度的前缀串。 该题因为模板串和匹配串一样, 所以很有特点, 很特殊。 请读者仔细品味。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 1000000 + 10;int T,n,m,f[maxn];char s[maxn];void getfail(char *p, int m) { f[0] = f[1] = 0; for(int i=1;i<m;i++) { int j = f[i]; while(j && p[i] != p[j]) j = f[j]; f[i+1] = p[i] == p[j] ? j+1 : 0; }}bool _find(char *T, char *p, int n, int m) { int j = 0; for(int i=0;i<n;i++) { while(j && p[j] != T[i]) j = f[j]; if(p[j] == T[i]) j++; if(j == m) return true; } return false;}int main() { scanf("%d",&T); while(T--) { scanf("%s",s); int len = strlen(s); getfail(s, len); int j = f[len], ans = 0; while(j) { if(len >= 3*j && _find(s+j, s, len-2*j, j)) { ans = j; break; } else j = f[j]; } printf("%d\n",ans); } return 0;}
1 0
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section(KMP)
- 【HDU】 4763 Theme Section (KMP)
- hdu 4763 Theme Section (简单KMP)
- HDU 4763 Theme Section (KMP)
- 【HDU 4763】Theme Section(KMP)
- hdu 4763 Theme Section(kmp)
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section (kmp | exkmp)
- HDU 4763 Theme Section(KMP)
- HDU 4763 Theme Section(kmp)
- KMP-hdu-4763-Theme Section
- hdu 4763 Theme Section(KMP)
- hdu 4763 Theme Section kmp
- HDU 4763 Theme Section(KMP)
- hdu 4763 Theme Section(KMP)
- hdu 4763 Theme Section KMP
- HDU 4763 Theme Section(KMP)
- Exynos4412 中断驱动开发相关问题总结
- 160. Intersection of Two Linked Lists
- Android窗口显示:全屏与隐藏标题栏
- android 选择头像上传
- 串口通信
- HDU 4763 Theme Section(KMP)
- 基础篇(十四) 进程间通信
- 36. Valid Sudoku
- WebRTC in the real world: STUN, TURN and signaling
- JavaScript下拉列表的二级联动
- 88. Merge Sorted Array
- 【暴力】HDU4858项目管理
- android学习之路(三)高级组件之图片切换器<ImageSwicther/>以及final关键字的基本用法
- 通过百度BAE搭建微信二次开发的服务(2)