【模式串个数匹配】+ KMP
2017-04-10 21:18
169 查看
实现代码 :
#include<iostream> #include<cstring> using namespace std; typedef struct{ int l; char c[100]; }node; int nx[100]; void pz(node &p,char *s){ if(strlen(s) > 100) return ; p.l = strlen(s); for(int i = 0 ; i < p.l; i++) p.c[i] = *(s + i); } void qn(node p){ nx[1] = 0; for(int i = 1,j = 0; i < p.l; i++){ j = nx[i]; while(j && p.c[i] != p.c[j]) j = nx[j]; nx[i + 1] = p.c[i] == p.c[j] ? j + 1 : 0; } } int kmp(node a,node b,int pl){ for(int i = pl,j = 0; i < b.l; i++){ while(j && a.c[j] != b.c[i]) j = nx[j]; if(a.c[j] == b.c[i]) j++; if(j == 3) return i + 1; } return 0; } int main() { node s; pz(s,"bbaaabbababba"); node T; qn(s); pz(T,"abb") ; int num = 0,pl = 0; while(s.l - pl >= T.l){ pl = kmp(T,s,pl); if(!pl) break; num++; } cout << num << endl; return 0; }
相关文章推荐
- 字符串模式匹配
- Sql Server 中的模式匹配
- 哈哈,原来模式匹配就是一个 strstr(string, str);
- 串的模式匹配算法之一:简单算法
- 模式匹配 KMP算法
- 串的模式匹配算法之二:首尾匹配算法
- Perl教学 第六篇 模式匹配
- 正则表达式的3种匹配模式
- 又学邹老大算法,模式匹配有重感昌!!!!
- SQL Server中的模式匹配
- 字符串普通模式匹配和KMP模式匹配[面相对象喽]
- KMP字符串模式匹配详解
- kmp 模式匹配。。。。
- KMP模式匹配算法以及普通模式匹配算法
- C#中关于模式匹配的简单例子
- 模式匹配---KMP算法
- KMP字符串模式匹配详解
- 正则表达式的3种匹配模式
- 突破该死的无回溯模式匹配算法
- 模式匹配的KMP算法详解