KMP算法
来源:互联网 发布:淘宝规则有哪些 编辑:程序博客网 时间:2024/06/05 15:30
#include <iostream> #include <string> #include <vector> using namespace std; void getNext(const string &substr, vector<int> &next) { next.clear(); next.resize(substr.size()); int j = -1; next[0] = -1; for(int i = 1; i < substr.size(); ++i) { while(j > -1 && substr[j + 1] != substr[i]) j = next[j]; if(substr[j + 1] == substr[i]) ++j; next[i] = j; } } void kmp(const string &str, const string &substr, vector<int> &next) { int cnt = 0; getNext(substr, next); int j = -1; for(int i = 0; i < str.size(); ++i) { while(j > -1 && substr[j + 1] != str[i]) j = next[j]; if(substr[j + 1] == str[i]) ++j; if(j == substr.size() - 1) { cout << "find in position" << i << endl; ++cnt; j = next[j]; } } if(cnt == 0) cout << "not find" << endl; } int main() { string str, substr; cin >> str >> substr; vector<int> next; kmp(str, substr, next); return 0; }转载自http://blog.csdn.net/a363344923/article/details/41788015
阅读全文
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- BZOJ 2393 容斥原理
- 过路费 (最短路)
- 马斯克OpenAI实验室的17岁高中生
- yanzheng
- CSDN MarkDown使用
- KMP算法
- Spring Aop拦截controller配置注意事项
- LeetCode基础-链表
- Intellij IDEA的SVN使用方式
- Ubuntu 16.04 LTS 挂起休眠睡眠,死机,宕机
- MYSQL企业常用架构与调优经验分享
- the first_blog
- Python爬虫——模拟登陆爬取csdn页面
- Java学习--基础类和容器