hihoCoder 1015 kmp算法
2016-04-09 22:31
453 查看
入门题= =
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int MAX = 1e4 + 5; int nex[MAX]; char str[MAX], p[100 *MAX]; void get_nex(char* arr, int* nex) { int k = -1, j = 0; nex[j] = k; int lenth = strlen(arr); while (j < lenth) { if (k == -1 || arr[j] == arr[k]) { ++j; ++k; if (arr[j] == arr[k]) nex[j] = nex[k]; else nex[j] = k; } else { k = nex[k]; } } } int kmp(char* p, char* str) { get_nex(str, nex); int res = 0; int lenth1 = strlen(p); int lenth2 = strlen(str); int i = 0, j = 0; while (i < lenth1) { if (j == -1 || p[i] == str[j]) { i++; j++; if (j == lenth2) { //j = 0; res++; j = nex[j]; } } else { j = nex[j]; } } return res; } int main() { int t; scanf("%d", &t); while (t--) { memset(nex, 0, sizeof(nex)); scanf("%s%s", str, p); printf("%d\n", kmp(p, str)); } return 0; }
相关文章推荐
- bzoj2330【SCOI2011】糖果
- java实现排列组合
- 操作系统开发系列—4.LDT
- iOS彩票项目--第二天,自定义蒙版、封装活动菜单、自定义pop菜单
- 解决pip install 时locale.Error: unsupported locale setting
- 关于Opencv编程入门的基础知识点
- 130. Surrounded Regions
- 第六次作业
- Timus 1049 Brave Balloonists
- bzoj3930【CQOI2015】选数
- android面试经典(6)
- c++作业3
- Spark 1.5新特性介绍
- SSH面试题收藏
- Bencode的编码与解码
- Java配置环境变量
- Java中的hashCode二
- Linux内核如何装载和启动一个可执行程序
- 辉光特效
- 扩展型模式之Visitor(访问者)模式