串的模式匹配KMP算法
来源:互联网 发布:java agent 做监控 编辑:程序博客网 时间:2024/05/18 10:14
#include<iostream>
using namespace std;
typedef struct
{
char*ch;
int length;
}SString;
int Ass(SString &H,char *sh){
int i=0;
while(sh[i]) i++;
H.ch=(char*)malloc((i+1)*sizeof(char));
H.length=i;
for(i=1;i<=H.length;i++)
H.ch[i]=sh[i-1];
return 0;
}
int Next(SString S,int * next){
int i=1,j=0;
next[1]=0;
while(i<=S.length)
{
if(j==0||S.ch[i]==S.ch[j]){i++;j++;next[i]=j;}
else j=next[j];
}
return 0;
}
int KMP(int *next,SString S,SString H){
int j=1,i=1;
while(i<=S.length&&j<=H.length){
if(S.ch[i]==H.ch[j]){i++;j++;}
else{
if(!next[j]) i++;
else j=next[j];
}
}
if(j>H.length) cout<<"success";
else cout<<"fail";
return 0;
}
int main(){
SString H,S;
char*a="beijing";
Ass(H,a);
int i=1;
while(i<=H.length)
{cout<<H.ch[i];i++;}
cout<<endl;
char*b="jing";
Ass(S,b);
i=1;
while(i<=S.length)
{cout<<S.ch[i];i++;}
cout<<endl;
int *next;
next=(int*)malloc((S.length+1)*sizeof(int));
Next(S,next);
KMP(next,H,S);
cout<<endl;
return 0;
}
0 0
- 串的模式匹配KMP算法
- 串的模式匹配 经典KMP算法
- 串的模式匹配KMP算法
- 串的模式匹配改进--KMP算法
- 串的模式匹配-KMP算法
- 串的模式匹配KMP算法模板
- 【数据结构】 串的模式匹配算法KMP
- 串的模式匹配算法(KMP)
- 串的模式匹配---------kmp算法
- 串的模式匹配:KMP算法
- 串的模式匹配-KMP算法
- 串的模式匹配KMP算法
- KMP算法--串的模式匹配
- 串的模式匹配算法-KMP
- 串的模式匹配-KMP算法
- 模式匹配的KMP 算法
- 模式匹配的算法Kmp
- 串的模式匹配(基于KMP的匹配算法)
- 设备树的一些知识点
- Charles--使用Charles抓Https包
- oracle创建用户并授权
- Educational Codeforces Round 8 E - Zbazi in Zeydabad
- RC522在S5PV210处理器上的linux驱动调试(1)
- 串的模式匹配KMP算法
- osg节点访问和遍历
- SSH 公钥私钥认证
- 约瑟夫环--数组实现
- hdoj The sum problem 2058 (数学等差公式&技巧转换)
- Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missi
- Gradle for Android(二)全局设置、自定义BuildConfig、混淆
- 解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
- acm_problem_1013