KMP

border⁡\operatorname{border}:一个字符串的最长公共前后缀的长度。

∀border⁡\forall \operatorname{border},∃\exists 一个长度为 ∣S∣−border⁡\lvert S \rvert - \operatorname{border} 的周期。

易证。

可用于转化求周期等问题。

模板1 -by lzx

#include <iostream>
#include <cstring>
using namespace std;
string s,t;
int n,m,p[1000000];
int main()
{
	cin>>s>>t;
	n=s.size();
	m=t.size();
	s=' '+s;
	t=' '+t;
	p[0]=0;
	for(int i=1,j=0;i<m;i++)
	{
		while(j!=0&&t[i+1]!=t[j+1]) j=p[j];
		if(t[i+1]==t[j+1]) p[i+1]=++j;
		else p[i+1]=j;
	}
	for(int i=1,j=0;i<=n;i++)
	{
		while(j!=0&&s[i]!=t[j+1]) j=p[j];
		if(s[i]==t[j+1]) j++;
		if(j==m)
		{
			cout<<i-m+1<<endl;
			j=p[j];
		}
	}
	for(int i=1;i<=t.size()-1;i++)
	{
		cout<<p[i]<<" ";
	}
	return 0;
}

模板2 -by xjs

#include <iostream>
#include <cstring>
using namespace std;
string s,t;
int n,m,p[1000000];
int main()
{
	cin>>s>>t;
	n=s.size();
	m=t.size();
	s=' '+s+'#';
	t=' '+t+'$';
	p[1]=0;
    for(int i=2,j=1;i<=m;i++)
    {
        while(j!=1&&t[i]!=t[j]) j=p[j-1]+1;
        if(t[i]==t[j]) p[i]=j++;
        else p[i]=0;
    }
    int ps=1,pt=1;
    while(ps<=n)
    {
        //cout<<ps<<' '<<pt<<'\n';
        if(s[ps]==t[pt])
        {
            ps++;
            pt++;
            if(t[pt]=='$')
            {
                cout<<ps-m<<"\n";
            }
        }
        else
        {
            if(pt==1) ps++;
            else pt=p[pt-1]+1;
        }
    }
	for(int i=1;i<=m;i++)
	{
		cout<<p[i]<<" ";
	}
	return 0;
}

模板3 -神秘做法/jk

#include <iostream>
#include <cstring>
using namespace std;
string s,t;
int n,m,p[2000001];
int main()
{
	cin>>s>>t;
	n=s.size();
	m=t.size();
	t=' '+t+'$';
	s=t+s;
    //cout<<s<<"\n";
	p[1]=0;
    for(int i=2,j=1;i<=s.size()-1;i++)
    {
        while(j!=1&&s[i]!=s[j]) j=p[j-1]+1;
        if(s[i]==s[j]) p[i]=j++;
        else p[i]=0;
    }
    for(int i=m+1;i<=s.size()-1;i++)
    {
        if(p[i]==m)
        {
            cout<<i-m-m<<"\n";
        }
    }
	for(int i=1;i<=m;i++)
	{
		cout<<p[i]<<" ";
	}
	return 0;
}

字符串哈希

模数:

  • 10000000071000000007
  • 998244353998244353
  • 2642^{64} (unsigned long long\text{unsigned long long}自然溢出)

易被卡

  • 10000000031000000003
  • 10000000091000000009
  • 998244853998244853
  • 999999937999999937
  • 469762049469762049
  • 515880119515880119
  • 35714285713571428571
  • 1926080919260809
  • 1926081719260817
  • 2012083920120839
  • 2013114720131147
  • 1128113311281133
  • 1128113911281139
  • 1128114711281147
  • 1128119911281199
  • 1128331311283313
  • 1128473911284739
  • 1128991111289911
  • 2811994728119947
  • 7428119974281199
  • 5228119952281199
  • 4112899941128999
  • 4281199942811999
  • 3428119934281199
  • 9991369999913699
  • 9990434999904349
  • 9043499990434999
  • 8904349189043491
  • 8340499183404991
  • 3404009934040099
  • 3404060334040603
  • 3404980134049801
  • 3404990934049909
  • 3891089938910899
  • 5208919952089199
  • 5291089952910899
  • 1369999913699999
  • 90434999043499
  • 33404993340499
  • 81369918136991
  • 11128991112899
  • 1000001910000019
  • 999983999983
  • 934049934049
  • 313699313699
  • 136999136999

(混进了一些奇怪的东西?

upd:混进了一些正常的东西(?-by zjh\tiny\color{#333333}\text{-by\ zjh}

Trie 树

字典树和字符串的对应关系:

前缀 ⟺\Longleftrightarrow 祖先

公共前缀 ⟺\Longleftrightarrow 公共祖先

最长公共前缀 ⟺\Longleftrightarrow 最近公共祖先

ch[now][id] 存储节点 now 走 id 这条边到达的节点编号。

根结点编号为 11。

较基础,结构简单。

衍生:AC自动机 SAM自动机 PAM自动机 后缀树 回文树 序列自动机 +∞+\infty

01 字典树

存储类似线段树(?)按二进制存储,位数不足的在高位补 00。

暴论:01 字典树=动态开点线段树