KMP算法
来源:互联网 发布:阿里云邮箱 编辑:程序博客网 时间:2024/05/16 19:38
#include<iostream>
#include<assert.h>
using namespace std;
void getNext(const char* str,int next[])//获取next数组
{
assert(str!=NULL&&next!=NULL);
int i=1,j=0;
int len=strlen(str);
next[0]=0;
next[1]=1;
while(i<len)
{
if(j==0||str[i]==str[j-1])
{
++i;
++j;
next[i]=j;
}
else
j=next[j-1];
}
}
int kmp(const char *str,const char *des,int *next)//kmp字符串匹配
{
assert(str!=NULL&&des!=NULL&&next!=NULL);
int m=strlen(str);
int n=strlen(des);
int i=0,j=0;
while(i<m&&j<n)
{
if(j==0||str[i]==des[j])
{
if(str[i]==des[j])
{
i++;
j++;
}
else
i++;
}
else
j=next[j-1];
}
if(j>=n)
return i-n;
else
return -1;
}
int main()
{
char sub[]="abaabcac";
int length=strlen(sub);
int *p=new int[length];
char str[]="ccccabaabcaccc";
int data=kmp(str,sub,p);
cout<<data<<endl;
system("pause");
}
#include<assert.h>
using namespace std;
void getNext(const char* str,int next[])//获取next数组
{
assert(str!=NULL&&next!=NULL);
int i=1,j=0;
int len=strlen(str);
next[0]=0;
next[1]=1;
while(i<len)
{
if(j==0||str[i]==str[j-1])
{
++i;
++j;
next[i]=j;
}
else
j=next[j-1];
}
}
int kmp(const char *str,const char *des,int *next)//kmp字符串匹配
{
assert(str!=NULL&&des!=NULL&&next!=NULL);
int m=strlen(str);
int n=strlen(des);
int i=0,j=0;
while(i<m&&j<n)
{
if(j==0||str[i]==des[j])
{
if(str[i]==des[j])
{
i++;
j++;
}
else
i++;
}
else
j=next[j-1];
}
if(j>=n)
return i-n;
else
return -1;
}
int main()
{
char sub[]="abaabcac";
int length=strlen(sub);
int *p=new int[length];
char str[]="ccccabaabcaccc";
int data=kmp(str,sub,p);
cout<<data<<endl;
system("pause");
}
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- []趋势科技2015校园招聘
- Linux - 内存映射
- 杭电ACM 2016 数据的交换输出
- L 1 距离,曼哈顿距离
- 经典SQL语句大全
- KMP算法
- Ping命令使用技巧
- html文件中加注释
- 鸟哥的linux私房菜读书笔记----第四章 安装centos与多重引导小技巧
- 撒范德萨
- 我的一生
- 自定义Cocos2dx摇杆(增强Joystick),增加摇杆跟随用户触点作为摇杆坐标,让摇杆不再死板!
- 做 人 做 事 的 七 张 王 牌
- kernel 3.x install oss4(open source sound v4.x)