[HihoCoder]#1015 : KMP算法
2016-06-23 20:54
477 查看
华电北风吹
天津大学认知计算与应用重点实验室
2016-06-23
题目链接:http://hihocoder.com/problemset/problem/1015
题目分析:KMP模板代码。
结题报告:经典代码。
天津大学认知计算与应用重点实验室
2016-06-23
题目链接:http://hihocoder.com/problemset/problem/1015
题目分析:KMP模板代码。
// problem1014.cpp : 定义控制台应用程序的入口点。 // KMP算法 // http://hihocoder.com/problemset/problem/1015 // 张正义 2016-04-12 #include "stdafx.h" #include <iostream> #include <vector> #include <string> using namespace std; vector<int> nextVector(string pattern) { int m = pattern.size(); vector<int> result(m); for (int i = 1; i < m; i++) { int j = result[i - 1]; while (j&&pattern[i] != pattern[j]) j = result[j - 1]; result[i] = pattern[i] == pattern[j] ? j + 1 : 0; } return result; } int KMP(string str, string pattern) { int n = str.size(), m = pattern.size(); vector<int> arr = nextVector(pattern); int count = 0; int p = 0; for (int i = 0; i < n; i++) { while (p>0 && pattern[p] != str[i]) p = arr[p - 1]; if (str[i] == pattern[p]) p++; if (p == m) count++; } return count; } int main(int argc, char* argv[]) { int n; cin >> n; string pattern, line; for (int i = 0; i < n; i++) { cin >> pattern; cin >> line; cout << KMP(line, pattern) << endl; } return 0; }
结题报告:经典代码。
相关文章推荐
- 不容易系列之(3)—— LELE的RPG难题 +多个递推式解法
- 字符串
- 单元测试
- hdu2047 阿牛的EOF牛肉串 多个递推式解法
- BZOJ_1619_[Usaco2008_Nov]_Guarding_the_Farm_保卫牧场_(模拟+bfs)
- Rsync命令详解
- Java IO流的一个小工具
- git 基本操作
- 解决centos7安装wmwaretools找不到kernel header
- item2,实现singleton模式
- hdu 2068 RPG的错排
- Java 4种方法实现选择排序算法
- Android SDK下载速度慢的解决方法(简单使用代理)
- HDU1257 最少拦截系统 贪心算法 (多人乘船问题)水题
- Android Studio项目目录结构介绍
- HTTP请求方法
- 数据库备份checksum选项你会用么?
- ubuntu启动无桌面图标?
- [HihoCoder]#1014 : Trie树
- Android命令行创建并运行 AVD模拟器