KMP算法的简明实现
2011-03-23 14:10
369 查看
kmp算法简明实现
#include<iostream>#include<string.h>
using namespace std;
int main()
{
char t[10], s[20];
cin>>s>>t;
int next[11], d = strlen(t), k = strlen(s);
int i = 0, j = -1, l = 0;
next[0] = -1;
while(i < d)
{
if(j == -1 || t[i] == t[j])
{
i++;j++;
next[i] = j;
}
else
j = next[j];
}
for(int w=0; w < d; w++) //显示各next值
cout<<next[w]<<endl;
i = 0;
j = 0;
while(i < k && j < d)
if(j == -1 || s[i] == t[j])
{
i++;
j++;
}
else
j = next[j];
if(j == d)
l = i - d;
if(l) cout<<l<<endl;
else cout<<"fail"<<endl;
return 0;
}本文出自 “璨夏爱程序” 博客,转载请与作者联系!
相关文章推荐
- 【leetcode KMP算法实现】Implement strStr()
- 字符串——KMP算法实现
- php实现简明英汉词典
- [简明Python教程3.2版本实现] try_except.py
- KMP算法java实现
- java实现的kmp算法
- kmp算法及其c++实现
- KMP算法理解与实现
- KMP算法实现
- KMP算法的c++实现
- KMP算法详解及C++实现
- java实现子字符串的KMP算法
- python的KMP算法实现
- 凭借对KMP算法的了解,用java实现了一下,结果和java自带的字符串indexOf比,性能差了十倍。。。
- KMP算法的一个C++实现
- strstr实现(练习KMP算法的好例子)
- 算法:KMP算法实现
- KMP算法的实现
- Leetcode28. kmp算法实现字符串匹配
- 【模式匹配】KMP算法的简明理解