KMP算法模板

KMP算法模板

自己对KMP算法不是很熟,特意写了一篇KMP模拟。

P3375

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define FR freopen("in.txt", "r", stdin)
#define FW freopen("out.txt", "w", stdout)

struct KMP
{
  int next[1000005];
  string pattern;

  void init(const string &a)
  {
    next[0] = 0;
    for (int i = 1; i < a.size(); i++)
    {
      int p = next[i - 1];
      while (p != 0 && a[i] != a[p])
      {
        p = next[p - 1];
      }

      next[i] = a[i] == a[p] ? p + 1 : 0;
    }
    pattern = a;
  }

  void printNext() const
  {
    for (int i = 0; i < pattern.size(); i++)
      printf("%d ", next[i]);
  }

  void match(const string &text) const
  {
    int i = 0;
    int j = 0;
    while (j < text.size())
    {
      if (pattern[i] == text[j])
      {
        i++;
        j++;
        if (i == pattern.size())
        {
          // OK
          printf("%d\n", j - i + 1);
          i = next[i - 1];
        }
      }
      else
      {
        if (i == 0)
          j++;
        else
          i = next[i - 1];
      }
    }
  }
} kmp;

int main()
{
  string s1, s2;
  cin >> s1 >> s2;
  kmp.init(s2);
  kmp.match(s1);
  kmp.printNext();
  return 0;
}

例题

P4391 最小循环节

结论 n − n e x t [ n ] n - next[n] nnext[n]

#include<cstdio>
using namespace std;
const int maxn=1111111;
int n,kmp[maxn];//kmp数组即next数组
char ss[maxn];
int main()
{
	scanf("%d%s",&n,ss+1);
	int j=0;
	for(int i=2;i<=n;++i)
	{
		while(j&&ss[i]!=ss[j+1]) j=kmp[j];
		if(ss[i]==ss[j+1]) ++j;
		kmp[i]=j;
	}
	printf("%d",n-kmp[n]);
	return 0;
}

P3435 最短前缀数组

#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;

int nxt[1000005];
int mit[1000005];
ll ans = 0;

void kmp(string &str)
{
    for (int i = 1; i < str.size(); i++)
    {
        int curr = nxt[i - 1];
        while (curr > 0 && str[curr] != str[i])
            curr = nxt[curr - 1];
        nxt[i] = str[curr] == str[i] ? curr + 1 : 0;
    }
    for (int i = 1; i < str.size(); i++)
    {
        int j = nxt[i];
        for (; j != 0 && nxt[j - 1] != 0; j = nxt[j - 1])
            ;
        nxt[i] = j;
        if (j != 0)
            ans += i - j + 1;
    }
}

int main()
{
    int n;
    cin >> n;
    string str;
    cin >> str;

    kmp(str);
    cout << ans;
    return 0;
}

P4824 删除粘连字符串

#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;

int nxt[50];
int sta[50];
int match[50];
int top = 0;

void kmp(string &str)
{
    for (int i = 1; i < str.size(); i++)
    {
        int curr = nxt[i - 1];
        while (curr > 0 && str[curr] != str[i])
            curr = nxt[curr - 1];
        nxt[i] = str[i] == str[curr] ? curr + 1 : 0;
    }
}

int main()
{
    string txt, pat;
    cin >> txt >> pat;
    kmp(pat);
    int j = 0;
    for (int i = 0; i < txt.size(); i++)
    {
        while (j > 0 && txt[i] != pat[j])
            j = nxt[j - 1];
        if (txt[i] == pat[j])
            j++;
        match[i] = j;
        sta[top++] = i;
        if (j == pat.size())
        {
            top -= j;
            j = match[sta[top - 1]];
        }
    }

    for (int i = 0; i < top; i++)
    {
        cout << txt[sta[i]];
    }
    return 0;
}

P2375 NOI例题

#include <bits/stdc++.h>
#define FR freopen("in.txt", "r", stdin)
using namespace std;
typedef unsigned long long ll;

ll nxt[1000005];
ll nxt2[1000005];
ll cnt[1000005];
ll ans[1000005];

ll mod = 1000000007;

void kmp(string &str)
{
    for (int i = 1; i < str.size(); i++)
    {
        int curr = nxt[i - 1];
        while (curr > 0 && str[curr] != str[i])
            curr = nxt[curr - 1];
        nxt[i] = str[i] == str[curr] ? curr + 1 : 0;
    }

    for (int i = 1; i < str.size(); i++)
    {
        cnt[i] = nxt[i] != 0 ? cnt[nxt[i] - 1] + 1: 0;
    }

    for (int i = 1; i < str.size(); i++)
    {
        int curr = nxt2[i - 1];
        while (curr > 0 && (str[curr] != str[i] || (curr + 1) * 2 > i + 1))
            curr = nxt[curr - 1];
        nxt2[i] = str[i] == str[curr] ? curr + 1 : 0;
    }

    for (int i = 1; i < str.size(); i++)
    {
        ans[i] = nxt[i] != 0 ? cnt[nxt2[i] - 1] + 1 : 0;
    }
}

void solve()
{
    string str;
    cin >> str;

    kmp(str);
    ll Ans = 1;
    for (int i = 1; i < str.size(); i++)
    {
        Ans = (Ans * (ans[i] + 1)) % mod;
    }
    cout << Ans << endl;
}

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值