第三章使用数据结构 3.3.2字符串 kmp (大白书)

3.3 字符串

3.3.2Kmp

拓展kmp

模板

void getnxt(char *p){
    int m = strlen(p);
    int i=0, j=-1;
    //i,j表示当前比较的位置。
    nxt[0]=-1;///这里第一次码时,没加。
    while(i<m){
        if(j==-1||p[i]==p[j])nxt[++i]=++j;
     //这里不仅代表位置,假如相等后,j还代表长度。   
        else j=nxt[j];
    }
}
bool kmp(char *p, char *s){
    int n=strlen(s),m=strlen(p);
    int i=0,j=0;
    while(i<n){
        if(j==-1||s[i]==p[j])i++,j++;
        else j=nxt[j];
        if(j==m)return true;
    }
    return false;
}

模板题

next应用循环节。

ac自动机

模板题

#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define ALL(x) x.begin(),x.end()
#define mp make_pair
#define fi first
#define se second
#define db double
#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;

const db eps = 1e-8;
const db pi = acos(-1.0);

int read() {
  int x = 0, f = 0; char ch = getchar();
  while (!isdigit(ch)) f |= ch == '-', ch = getchar();
  while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
  return f ? -x : x;
}
template<typename T> void print(T x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) print(x / 10);
  putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
  print(x), putchar(let);
}

const int maxn = 1e4+10;
const int maxm = 1e6+10;
int tr[maxn*55][26], cnt[maxn*55], nex[maxn*55], q[maxn*55];
int n, idx;
char str[maxm];

void insert(char* str){
    int p = 0;
    for(int i = 0;str[i]; i ++ ){
        int t = str[i] - 'a';
        if(!tr[p][t]) tr[p][t] = ++ idx;
        p = tr[p][t];
    }
    cnt[p]++;
}

void build(){
    int hh = 0, tt = -1;
    fori(i,0,26)
        if(tr[0][i]) q[++tt] = tr[0][i];
    while(hh <= tt){
        int t = q[hh ++ ];
        fori(i, 0, 26){
            int p  = tr[t][i];
            if(!p) tr[t][i] = tr[nex[t]][i];
            else {
                nex[p] = tr[nex[t]][i];
                q[ ++ tt] = p;
            }
        }
    }
}

void init(){
    mst(tr,0);
    mst(cnt,0);///统计当前结点的单词个数
    mst(nex,0);
    idx = 0;

    n = read();
    fori(i,0,n){
        scanf("%s", str);
        insert(str);
    }

    build();
}

void sol(){
    init();
    int res = 0;
    scanf("%s", str);
    for(int i = 0, j = 0; str[i]; i ++ ){
        int t = str[i] - 'a';
        j = tr[j][t];

        int p = j;
        while(p){
            res += cnt[p];
            cnt[p] = 0;
            p = nex[p];
        }
    }
    printf("%d\n", res);
}

//#define LOCAL
int main()
{
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
    int tt;
    tt = read();
    while(tt--)sol();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值