数据结构-模式匹配串算法(KMP)
2024-09-30 14:35:52
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
void getnext(char *t, int *next, int lent)
{
int i = 0, j = -1;
next[0] = -1;
while (i < lent)
if (j == -1 || t[i] == t[j])
++i, ++j, next[i] = j;
else
j = next[j];
}
int kmp(char *s, char *t, int *next, int lens, int lent)
{
int i = 0, j = 0;
while (i < lens)
{
if (-1 == j || s[i] == t[j])
i++, j++;
else
j = next[j];
if (j == lent) return 1;
}
return 0;
}
int main()
{
char t[100],s[1000];
int next[100];
printf("Enter模式串,主串\n");
scanf("%s",t);
gets(s);
int lent = strlen(t);
int lens = strlen(s);
getnext(t, next, lent);
printf("%d\n1表示成功", kmp(s, t, next, lens, lent));
return 0;
}
最新文章
- R中数据拆分和整合
- 二 Java利用等待/通知机制实现一个线程池
- Mysql与Redis的同步实践
- Linux休眠,挂起,待机,关机的区别及相关命令
- C#语法杂谈
- MVC学习系列——ActionResult扩展
- 反射---Java高级开发必须懂的
- windows+apache2.2.9+php5.4.41+mysql安装
- jcSQL词法分析器对字符串token的解析
- js移动端向左滑动出现删除按钮
- java中volatile的简单理解
- 【XSY2715】回文串 树链剖分 回文自动机
- android 事件反拦截
- .NET持续集成与自动化部署之路第二篇——使用NuGet.Server搭建公司内部的Nuget(包)管理器
- 浅谈 equals 和 == 的区别
- 小程序 wepy wx.createAnimation 向右滑动渐入渐出
- MapServer和GeoServer对比
- The Begining
- MIT6.006Lec02:DocumentDistance
- linux 同步IO: sync、fsync与fdatasync
热门文章
- jQuery jQuery on()方法
- Spring Cloud(2):搭建Eureka
- 从零开始的全栈工程师——js篇2.4
- ArrayList,Vector, LinkedList 的存储性能和特性
- Android中渐变图片失真的解决方案
- 《Python高效开发实战》实战演练——基本视图3
- 新客户上云 - 来自 Azure 技术支持部门的忠告
- jQuery-显示与隐藏不用判断的快捷方法
- [Asp.Net] Global.asax
- linux 命令——48 watch (转)