HDU 1686 - Oulipo(kmp)
2015-10-21 20:59
375 查看
Problem link
分析:
kmp匹配
代码:
#include <cstdio> #include <cstring> const int maxn = 1000010; char w[maxn], t[maxn]; int _next[maxn]; void get_next() { int w_len = strlen(w); int i = 0, j; _next[0] = j = -1; while (i < w_len) { if (j == -1 || w[j] == w[i]) { i++; j++; _next[i] = j; } else { j = _next[j]; } } } int get_times() { int ans = 0; int w_len = strlen(w); int t_len = strlen(t); int i = 0, j = 0; while (j < t_len) { if (i == -1 || w[i] == t[j]) { i++; j++; } else { i = _next[i]; } if (i == w_len) { ans++; i = _next[i]; } } return ans; } int main() { int T; scanf("%d", &T); while (T--) { memset(w, 0, sizeof(w)); memset(t, 0, sizeof(t)); memset(_next, 0, sizeof(_next)); scanf("%s %s", w, t); get_next(); printf("%d\n", get_times()); } return 0; }
相关文章推荐
- [笔记] Convex Optimization 2015.10.14
- LUA中的数学库介绍
- 用vs2010环境下的MPICH2编程的几个注意点(附:无法include头文件的原因)
- 自定义滚动条
- android小问题: Notification通知栏 中用Intent传值无效
- ruby gem管理
- 字符串的常见操作
- 深入理解java异常处理机制
- [笔记] Convex Optimization 2015.09.30
- java一步一脚印—java基本类型的读写
- Trie树的两种实现 - hiho一下
- 练习
- STL - 容器 - Set
- kvc 定义 model
- Android读书笔记-----View动画
- C++学习——模板与异常处理
- IOS开发笔记4-流程控制/基本语句
- week7---10月21日 DIV模型
- Android 【百度地图】 基础配置(1)
- 主线程上延时,使用postDelayed