C++实现KMP算法(修正版,C++风格)
来源:互联网 发布:javascript和php哪个好 编辑:程序博客网 时间:2024/04/20 02:10
/* * Main.cpp * * Created on: Oct 7, 2015 * Author: chris */#include<iostream>#include<vector>#include<string>using namespace std;void get_next(const string pat, vector<int>& next){next.resize(pat.length(), 0);int i = 1, j = 0;while(i < pat.length()-1) {if(pat[i] == pat[j]) // matched.next[++i] = ++j; // record next.else if (j != 0) // doesn't matched.j = next[j]; // back trace.else // nomatched.next[++i] = 0;}//while}void Index_KMP(const string str, const string pat, vector<int>& inds){vector<int> next;get_next(pat, next);inds.clear();int i = 0, j = 0;while(i < str.length()) {if(str[i] == pat[j]){ // matched.++i; ++j; // move on.if(j >= pat.length()) {inds.push_back(i - pat.length());j = 0;//reset.}}else if ( j != 0 ) // back trace.j = next[j];else // impossible.++i;}//while}int main(void){string str1, str2;while(cin >> str1 >> str2) {vector<int> inds;Index_KMP(str1, str2, inds);for(int i = 0; i < inds.size(); ++i) {cout << inds[i] << " ";}cout << endl;cout.flush();}//while.return 0;}
0 0
- C++实现KMP算法(修正版,C++风格)
- C++实现KMP算法(C风格)
- KMP算法(c++)实现
- KMP算法C实现
- KMP算法c实现
- KMP算法 C实现
- [C++]KMP算法实现
- KMP算法(C++)
- KMP算法c语言实现
- KMP算法C#/c++实现
- KMP算法C代码实现
- KMP算法--c语言实现
- KMP算法C代码实现
- [转]KMP算法实现(源码c/c++)
- 常见排序算法(冒泡,选择,快速)的C语言实现【修正版】
- kmp算法实现-算法导论C语言版
- 蓝桥杯 算法提高 c++_ch04_02_修正版
- 算法提高 c++_ch04_02_修正版
- /*自守数 如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数。*/
- 第六周--项目三--括号的匹配
- android wifi连接与断开
- acmPOJ--1502
- hadoop之yarn Protocol Buffers
- C++实现KMP算法(修正版,C++风格)
- Mac下搭建SVN服务器
- myeclipse 使用JDBC方法直接访问sql2005
- 第2章 linux学习方法
- Oracle 左连接、右连接、全外连接、(+)号作用
- 51nod 1051 最大子矩阵和 (dp_good)
- POJ 题目3417 Network(LCA转RMQ+树形DP)
- 选择器
- Excel函数、快捷键等