#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;
}
数据结构-模式匹配串算法(KMP)
最新推荐文章于 2023-09-16 06:00:00 发布
本文介绍了一种高效的字符串搜索算法——KMP算法,并通过C++代码实现了该算法。KMP算法利用部分匹配表(next数组)来避免重复比较,提高了搜索效率。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
ACE-Step
音乐合成
ACE-Step
ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言
3058

被折叠的 条评论
为什么被折叠?



