数据结构 KMP
来源:互联网 发布:淘宝 发票 搜索什么 编辑:程序博客网 时间:2024/05/22 13:08
#include<stdio.h>
#include<string.h>
char marray[56],sarray[56];
int next[56];
void get_next()
{
int m,s,t;
next[0]=-1;
m=0;
s=-1;
while(m<strlen(sarray))
{
if(s==-1||sarray[s]==marray[m])
{
s++;
m++;
if(sarray[s]==sarray[m])
next[m]=next[s];
else
next[m]=s;
}
else
s=next[s];
}
}
int KMP()
{
int lenm=strlen(marray);
int lens=strlen(sarray);
int m=0;
int s=0;
while(m<lenm&&s<lens)
{
if(s==-1||marray[m]==sarray[s])
{
s++;
m++;
}
else
s=next[s];
}
if(s<lens)
return -1;
else
return m-lens;
}
int main()
{
while(1)
{
scanf("%s%s",marray,sarray);
printf("%s\n%s\n",marray,sarray);
get_next();
int t=KMP();
for(int i=0;i<strlen(sarray);i++)
printf("%d\n",next[i]);
if(t<0)
printf("No\n");
else
printf("%d\n",t);
}
return 0;
}
#include<string.h>
char marray[56],sarray[56];
int next[56];
void get_next()
{
int m,s,t;
next[0]=-1;
m=0;
s=-1;
while(m<strlen(sarray))
{
if(s==-1||sarray[s]==marray[m])
{
s++;
m++;
if(sarray[s]==sarray[m])
next[m]=next[s];
else
next[m]=s;
}
else
s=next[s];
}
}
int KMP()
{
int lenm=strlen(marray);
int lens=strlen(sarray);
int m=0;
int s=0;
while(m<lenm&&s<lens)
{
if(s==-1||marray[m]==sarray[s])
{
s++;
m++;
}
else
s=next[s];
}
if(s<lens)
return -1;
else
return m-lens;
}
int main()
{
while(1)
{
scanf("%s%s",marray,sarray);
printf("%s\n%s\n",marray,sarray);
get_next();
int t=KMP();
for(int i=0;i<strlen(sarray);i++)
printf("%d\n",next[i]);
if(t<0)
printf("No\n");
else
printf("%d\n",t);
}
return 0;
}
0 0
- 数据结构 KMP
- [数据结构]KMP算法实现
- 数据结构 模式匹配(KMP)
- 数据结构笔记-KMP算法
- [数据结构]KMP小结
- 《数据结构》KMP实现
- 数据结构 KMP算法代码
- [数据结构] KMP字符串匹配
- 数据结构--KMP算法
- [数据结构]字符串匹配KMP
- 【暑假】[实用数据结构]KMP
- 【数据结构】KMP算法
- 数据结构—KMP模板
- 数据结构关于KMP算法
- 数据结构--KMP简单应用
- 数据结构--KMP应用
- 数据结构-字符串匹配kmp
- 【数据结构基础】KMP算法
- c语言学习中遇到的问题
- 关于strcmp比较的问题
- ubuntu 12.04 vim gedit中文乱码解决
- Roman to Integer
- FZU 2092 记忆化搜索 || BFS
- 数据结构 KMP
- JAVA四种引用常量
- v4l2的开发详细步骤
- java 集合-001 Set
- Google Maps API V2 for Android 上路一之配置地图
- 矩阵乘法专题1——bzoj 1297 [SCOI2009] 迷路题解
- java classLoader体系结构使用详解
- v4l2_field理解
- Struts2学习笔记——Struts2与Spring整合