KMP,字典树与AC自动机,哈希算法,01字典树

博客探讨了KMP算法中的失配指针概念,将其与AC自动机的fail指针进行了比较。AC自动机通过字典树构建,并利用fail指针优化匹配效率。在AC自动机的构建过程中详细描述了fail指针的初始化和查询过程。此外,还提及了哈希算法和01字典树在问题解决中的应用。
摘要由CSDN通过智能技术生成

KMP中的一个重要思想与AC自动机非常相似,就是失配指针。在kmp算法中指匹配失败后该返回到字符串的哪个位置,也就是俗称的特定位置的复活出生点。

KMP模板如下:

//#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include<queue>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
#define debug cout<<"procedures above are available"<<endl;
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll nextlong()
{
    ll tmp = 0, si = 1;
    char c;
    c = getchar();
    while (c > '9' || c < '0')
    {
        if (c == '-')
            si = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        tmp = tmp * 10 + c - '0';
        c = getchar();
    }
    return si * tmp;
}
/**Maintain your determination.Nobody knows the magnificent landscape
at his destination before the arrival with stumble.**/
/**Last Remote**/
string text,sub;
ll nexxt[500000];
void getnext()
{
    ll pt = 0, pt2 = -1;
    nexxt[0] = -1;//初始出生点为边缘外
    while (pt < sub.size())
    {
        if (pt2 == -1 || sub[pt2] == sub[pt])
        {
            nexxt[++pt] = ++pt2;//如果某个字母在字符串中重复出现,那么确定它的复活出生点
        }
        else
            pt2 = nexxt[pt2];//否则判定死亡,返回上一个出生点
    }
}
ll cnt = 0;
void kmp()
{
    getnext();
    ll pt1 = 0, pt2 = 0;
    while (pt1 < text.size())
    {
        //cout << pt1 << " " << pt2 << end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值