HDU - 2594 Simpsons’ Hidden Talents(KMP)
2015-10-26 22:53
381 查看
题目大意:问你前缀和后缀相等的最长长度
解题思路:KMP的裸题了,next数组的应用
解题思路:KMP的裸题了,next数组的应用
#include <cstdio> #include <cstring> const int N = 50010; char s1 , s2 ; int len1, len2; int next ; void getFail() { len1 = strlen(s1); len2 = strlen(s2); int i = 0, j = -1; next[0] = -1; while (i < len1) { if (j == -1 || s1[j] == s1[i]) { i++; j++; next[i] = j; } else j = next[j]; } } void solve() { int i = 0, j = 0; while (i < len2) { if (j == -1 || s1[j] == s2[i]) { i++; j++; if (i == len2) break; if (j == len1) j = next[j]; } else j = next[j]; } bool flag = false; for (int i = 0; i < j; i++) { printf("%c", s1[i]); flag = true; } if (flag) printf(" "); printf("%d\n", j); } int main() { while (scanf("%s", s1) != EOF) { scanf("%s", s2); getFail(); solve(); } return 0; }
相关文章推荐
- 枚举 _枚举的其他应用
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- HashMap工作原理
- Android编程宝典-第二章-Activity
- BOM对象
- 2015年10月15号到24号.net工程师面试题
- IOS--判断当前网络是否可用
- C++:类模板与模板类
- MMDrawerController抽屉侧边栏的简单使用
- Core Data系列三——基本使用
- 四,jquery事件绑定(bind)和取消事件(unbind)
- 【转】最大权闭合图
- second scrum meeting - 151026
- 实战Scala & Spark (3)
- 对于宽字符的初步运用
- POJ - 2752 Seek the Name, Seek the Fame(KMP next数组的理解)
- 4.1常用分布的概率函数图
- c++使用happyhttp发送http请求
- Jcrop_jQuery图片裁剪插件
- jquery 筛选元素 (2)