跳到正文

个人记录

7.11 ~ 7.15 trie,01-trie,KMP 总结

Maovow约 47 分钟
原文
文章信息与版本

文章信息

发布
2025年7月16日
收录
2026年8月16日
更新
2026年8月27日
篇幅
约 47 分钟

版本

  1. v1当前

前置知识

trie

就是将每个字符符串投射到一棵树上。 为了方便,会先搞一个超级节点 0 。 对于每个字符串 s ,先定义一个整型 now ,初始化为 0

  1. 查找 now 下有没有 s[i] 这个字符。
    • 若没有,新增一个节点,设为 ++id
  2. 将 now 设为 now 下 s[i] 的编号
  3. 若 i < s.size() 则回到第一步
  4. 将 now 打上一个 End 标记 字典树可以快速查找前缀类问题

01-trie

就是将一个整数当作字符串处理。 01-trie 拥有同深度、单调性等特点,常用于异或贪心里

KMP

前缀函数

定义

给定一个长度为 nn 的字符串 ss,其 前缀函数 被定义为一个长度为 nn 的数组 π\pi。 其中 π[i]\pi[i] 的定义是:

  1. 如果子串 s[0…i]s[0\dots i] 有一对相等的真前缀与真后缀:s[0…k−1]s[0\dots k-1] 和 s[i−(k−1)…i]s[i - (k - 1) \dots i],那么 π[i]\pi[i] 就是这个相等的真前缀(或者真后缀,因为它们相等)的长度,也就是 π[i]=k\pi[i]=k;
  2. 如果不止有一对相等的,那么 π[i]\pi[i] 就是其中最长的那一对的长度;
  3. 如果没有相等的,那么 π[i]=0\pi[i]=0。 简单来说 π[i]\pi[i] 就是,子串 s[0…i]s[0\dots i] 最长的相等的真前缀与真后缀的长度。 用数学语言描述如下: π[i]=max⁡k=0…i{k:s[0…k−1]=s[i−(k−1)…i]}\pi[i] = \max_{k = 0 \dots i}\{k: s[0 \dots k - 1] = s[i - (k - 1) \dots i]\} 特别地,规定 π[0]=0\pi[0]=0。

过程

举例来说,对于字符串 abcabcd, π[0]=0\pi[0]=0,因为 a 没有真前缀和真后缀,根据规定为 0 π[1]=0\pi[1]=0,因为 ab 无相等的真前缀和真后缀 π[2]=0\pi[2]=0,因为 abc 无相等的真前缀和真后缀 π[3]=1\pi[3]=1,因为 abca 只有一对相等的真前缀和真后缀:a,长度为 1 π[4]=2\pi[4]=2,因为 abcab 相等的真前缀和真后缀只有 ab,长度为 2 π[5]=3\pi[5]=3,因为 abcabc 相等的真前缀和真后缀只有 abc,长度为 3 π[6]=0\pi[6]=0,因为 abcabcd 无相等的真前缀和真后缀 同理可以计算字符串 aabaaab 的前缀函数为 [0,1,0,1,2,2,3][0, 1, 0, 1, 2, 2, 3]。

计算前缀函数的朴素算法

过程

一个直接按照定义计算前缀函数的算法流程:

  • 在一个循环中以 i=1→n−1i = 1\to n - 1 的顺序计算前缀函数 π[i]\pi[i] 的值(π[0]\pi[0] 被赋值为 00)。
  • 为了计算当前的前缀函数值 π[i]\pi[i],我们令变量 jj 从最大的真前缀长度 ii 开始尝试。
  • 如果当前长度下真前缀和真后缀相等,则此时长度为 π[i]\pi[i],否则令 j 自减 1,继续匹配,直到 j=0j=0。
  • 如果 j=0j = 0 并且仍没有任何一次匹配,则置 π[i]=0\pi[i] = 0 并移至下一个下标 i+1i + 1。 ——摘自 OI-Wiki 真不是我懒,是我自己都搞不太懂

P2580 于是他错误的点名开始了 - 洛谷

模板,不讲

查询和插入差不多,但是 now 节点下找不到的话要返回 0,最后还要看一下有没有 End 标记

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,id;
ll trie[500005][26];
bool End[500005],vis[500005];

void insert(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][x - 'a'] == 0) trie[now][x - 'a'] = ++id;
		now = trie[now][x - 'a'];
	}
	End[now] = 1;
	return ;
}

ll query(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][x - 'a'] == 0) return 0;
		now = trie[now][x - 'a'];
	}
	if(End[now]){
		if(vis[now]){
			return 2;
		}else{
			vis[now] = 1;
			return 1;
		}
	}
	return 0;
}

int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		insert(s);
	}
	ll T;
	cin >> T;
	while(T--){
		string s;
		cin >> s;
		ll tmp = query(s);
		if(tmp == 0) cout << "WRONG\n";
		if(tmp == 1) cout << "OK\n";
		if(tmp == 2) cout << "REPEAT\n";
	}
	return 0;
} 

SP4033 PHONELST - Phone List - 洛谷

模板

可以边插边查询有没有标记

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,id;
ll trie[100005][10];
bool End[100005];

bool insert(string s){
	ll now = 0,last = id;
	for(auto x:s){
		if(End[now] == 1) return 1;
		if(trie[now][x - '0'] == 0) trie[now][x - '0'] = ++id;
		now = trie[now][x - '0'];
	}
	End[now] = 1;
	return last == id;
}

void work(){
	for(int i = 0;i <= id;i++){
		for(int j = 0;j <= 9;j++){
			trie[i][j] = 0;
		}
		End[i] = 0;
	}
	id = 0;
	bool f = 0;
	cin >> n;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		f |= insert(s);
	}
	if(f) cout << "NO\n";
	else cout << "YES\n";
	return ;
}

int main(){
	ll T;
	cin >> T;
	while(T--) work();
	return 0;
} 

P2922 [USACO08DEC] Secret Message G - 洛谷

对于比他短的,直接在查找过程中,慢慢累加 End 比他长的,对于每个点都记录一个被经过的次数 cnt。 记得删掉重复部分(自己)

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,m,id;
ll trie[500005][26],cnt[500005],End[500005];

void insert(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][x - '0'] == 0) trie[now][x - '0'] = ++id;
		now = trie[now][x - '0'];
		cnt[now]++;
	}
	End[now]++;
	return ;
}

ll query(string s){
	ll now = 0,tmp = 0;
	for(auto x:s){
		if(trie[now][x - '0'] == 0) return tmp;
		now = trie[now][x - '0'];
		tmp += End[now];
	}
	return tmp + cnt[now] - End[now];
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0); 
	cin >> m >> n;
	for(int i = 1;i <= m;i++){
		ll l;
		cin >> l;
		string s = "";
		for(int i = 1;i <= l;i++){
			char c;
			cin >> c;
			s += c;
		}
		insert(s);
	}
	for(int i = 1;i <= n;i++){
		ll l;
		cin >> l;
		string s = "";
		for(int i = 1;i <= l;i++){
			char c;
			cin >> c;
			s += c;
		}
//		cout << "::";
		cout << query(s) << "\n";
	}
	return 0;
} 

P8306 【模板】字典树 - 洛谷

上题弱化版

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,m,id;
ll trie[3000005][63],cnt[3000005],End[3000005];

ll To(char c){
    if(c >= 'a' && c <='z') return c - 'a';
    else if(c >= 'A' && c <= 'Z') return c - 'A' + 26;
    return c - '0' + 52;
}

void insert(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][To(x)] == 0) trie[now][To(x)] = ++id;
		now = trie[now][To(x)];
		cnt[now]++;
	}
	End[now]++;
	return ;
}

ll query(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][To(x)] == 0) return 0;
		now = trie[now][To(x)];
	}
	return cnt[now];
}

void work(){
	for(int i = 0;i <= id;i++){
		for(int j = 0;j < 62;j++){
			trie[i][j] = 0;
		}
		cnt[i] = End[i] = 0;
	}
	id = 0;
	cin >> n >> m;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		insert(s);
	}
	for(int i = 1;i <= m;i++){
		string s;
		cin >> s;
		cout << query(s) << "\n";
	}
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T;
	cin >> T;
	while(T--) work();
	return 0;
} 

[P3065 USACO12DEC] First! G - 洛谷

枚举字符串,与同层的找大小关系,若有环,就不行。若前缀在数据中出现过,也不行。

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <vector>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,id = 0;
ll trie[300005][26],G[26][26],in[26];
bool End[300005];
string a[30005];
vector<string> ans;

void insert(string s){
	ll now = 0;
	for(auto x:s){
		if(trie[now][x - 'a'] == 0) trie[now][x - 'a'] = ++id;
		now = trie[now][x - 'a'];
	}
	End[now] = 1;
	return ;
}

bool topu(){
	ll cnt = 0;
	queue<ll> q;
	for(int i = 0;i < 26;i++){
		if(in[i] == 0){
			q.push(i);
		}
	}
	while(q.size()){
		ll x = q.front();
		q.pop();
		cnt++;
		for(int i = 0;i < 26;i++){
			if(G[x][i]){
				in[i]--;
				if(in[i] == 0){
					q.push(i);
				}
			}
		}
	}
	return cnt == 26;
}

bool check(string s){
	for(int i = 0;i < 26;i++){
		for(int j = 0;j < 26;j++){
			G[i][j] = 0;
		}
		in[i] = 0;
	}
	ll now = 0,len = 0;
	for(auto x:s){
		len++;
		for(int i = 0;i < 26;i++){
			if(i != x - 'a' && trie[now][i] != 0 && G[i][x - 'a'] == 0){
				G[i][x - 'a'] = 1;
				in[x - 'a']++;
			}
		}
		now = trie[now][x - 'a'];
		if(End[now] == 1 && len != s.size()) return 0;
	}
	return topu();
}

int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		string s;
		cin >> s;
		insert(s);
		a[i] = s;
	}
	for(int i = 1;i <= n;i++){
		if(check(a[i])){
			ans.push_back(a[i]);
		}
	}
	cout << ans.size() << "\n";
	for(auto x:ans){
		cout << x << "\n";
	}
	return 0;
}

Chip Factory - HDU 5536 - Virtual Judge

暴力

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,id;
ll a[1005],trie[31005][2],cnt[31005];

void insert(ll x){
	ll now = 0;
	for(int i = 31;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		if(trie[now][tmp] == 0) trie[now][tmp] = ++id;
		now = trie[now][tmp];
		cnt[now]++;
	}
	return ;
}

void erase(ll x){
	ll now = 0;
	for(int i = 31;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		now = trie[now][tmp];
		cnt[now]--;
	}
	return ;
}

ll query(ll x){
	ll now = 0,ans = 0;
	for(int i = 31;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		if(trie[now][tmp ^ 1] && cnt[trie[now][tmp ^ 1]] > 0){
			ans += (1 << i);
			now = trie[now][tmp ^ 1];
		}else{
			now = trie[now][tmp];
		}
	}
	return ans;
}

void work(){
	for(int i = 0;i <= id;i++){
		trie[i][0] = trie[i][1] = cnt[i] = 0;
	}
	id = 0;
//	ll T;
	cin >> n;
	for(int i = 1;i <= n;i++){
		ll x;
		cin >> x;
		insert(x);
		a[i] = x;
	}
	ll mx = -1e18;
	for(int i = 1;i <= n;i++){
		for(int j = i + 1;j <= n;j++){
			erase(a[i]);
			erase(a[j]);
			mx = max(mx,query(a[i] + a[j]));
			insert(a[i]);
			insert(a[j]);
		} 
	}
	cout << mx << "\n";
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T;
	cin >> T;
	for(int _ = 1;_ <= T;_++){
//		cout << "Case #" << _ << ":\n"; 
		work();
	}
	return 0;
}

Xor Sum - HDU 4825 - Virtual Judge

直接贪心,在树上找二进制不同的节点即可

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n,id;
ll trie[3300005][2];

void insert(ll x){
	ll now = 0;
	for(int i = 32;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		if(trie[now][tmp] == 0) trie[now][tmp] = ++id;
		now = trie[now][tmp];
	}
	return ;
}

ll query(ll x){
	ll now = 0,ans = 0;
	for(int i = 32;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		if(trie[now][tmp ^ 1]){
			ans += ((tmp ^ 1) << i);
			now = trie[now][tmp ^ 1];
		}else{
			ans += (tmp << i);
			now = trie[now][tmp];
		}
	}
	return ans;
}

void work(){
	for(int i = 0;i <= id;i++){
		trie[i][0] = trie[i][1] = 0;
	}
	id = 0;
	ll T;
	cin >> n >> T;
	for(int i = 1;i <= n;i++){
		ll x;
		cin >> x;
		insert(x);
	}
	while(T--){
		ll x;
		cin >> x;
		cout << query(x) << "\n";
	}
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T;
	cin >> T;
	for(int _ = 1;_ <= T;_++){
		cout << "Case #" << _ << ":\n"; 
		work();
	}
	return 0;
}

P4551 最长异或路径 - 洛谷

异或路径可以表示为两个端点到根。剩下的就是和上节课一样

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
struct edge{
	ll e,x;
};
ll n,id;
ll a[100005],trie[3100005][2];
vector<edge> G[100005];

void dfs(ll x,ll fa){
	for(auto nx:G[x]){
		if(nx.e != fa){
			a[nx.e] = a[x] ^ nx.x;
			dfs(nx.e,x);
		}
	}
	return ;
}

void insert(ll x){
	ll now = 0;
	for(int i = 31;i >= 0;i--){
		ll bit = (x >> i) & 1;
		if(trie[now][bit] == 0) trie[now][bit] = ++id;
		now = trie[now][bit];
	}
	return ;
}

ll query(ll x){
	ll now = 0,ans = 0;
	for(int i = 31;i >= 0;i--){
		ll bit = (x >> i) & 1;
		if(trie[now][bit ^ 1]){
			ans += (1ll << i);
			now = trie[now][bit ^ 1];
		}else{
			now = trie[now][bit];
		}
	}
	return ans;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i < n;i++){
		ll s,e,x;
		cin >> s >> e >> x;
		G[s].push_back({e,x});
		G[e].push_back({s,x});
	}
	dfs(1,0);
	for(int i = 1;i <= n;i++){
		insert(a[i]);
	}
	ll mx = -1e18;
	for(int i = 1;i <= n;i++){
		mx = max(mx,query(a[i]));
	}
	cout << mx;
	return 0;
}

Problem - 282E - Codeforces

几乎和上题一样 区间异或用异或前缀和即可

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
struct edge{
	ll e,x;
};
ll n,id;
ll a[100005],trie[3100005][2];
vector<edge> G[100005];

void dfs(ll x,ll fa){
	for(auto nx:G[x]){
		if(nx.e != fa){
			a[nx.e] = a[x] ^ nx.x;
			dfs(nx.e,x);
		}
	}
	return ;
}

void insert(ll x){
	ll now = 0;
	for(int i = 31;i >= 0;i--){
		ll bit = (x >> i) & 1;
		if(trie[now][bit] == 0) trie[now][bit] = ++id;
		now = trie[now][bit];
	}
	return ;
}

ll query(ll x){
	ll now = 0,ans = 0;
	for(int i = 31;i >= 0;i--){
		ll bit = (x >> i) & 1;
		if(trie[now][bit ^ 1]){
			ans += (1ll << i);
			now = trie[now][bit ^ 1];
		}else{
			now = trie[now][bit];
		}
	}
	return ans;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n;
	for(int i = 1;i < n;i++){
		ll s,e,x;
		cin >> s >> e >> x;
		G[s].push_back({e,x});
		G[e].push_back({s,x});
	}
	dfs(1,0);
	for(int i = 1;i <= n;i++){
		insert(a[i]);
	}
	ll mx = -1e18;
	for(int i = 1;i <= n;i++){
		mx = max(mx,query(a[i]));
	}
	cout << mx;
	return 0;
}

Problem - 665E - Codeforces

和上题差不多,但是若又可以选 0 又可以选 1 ,那么先加上 1 的答案,再选 0 继续深入。 统计有多少节点经过这个节点,1 的答案就是 1 子树根节点的经过次数

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
int n,m,id = 0,sum;
int trie[31000005][2],cnt[31000005];
ll ans;

void insert(int x){
	int now = 0;
	for(int i = 31;i >= 0;i--){
		int bit = (x >> i) & 1;
		if(trie[now][bit] == 0) trie[now][bit] = ++id;
		now = trie[now][bit];
		cnt[now]++;
	}
	return ;
}

int query(int x){
	int now = 0,tmp = 0;
	for(int i = 31;i >= 0;i--){
		int bit = (x >> i) & 1,BIT = (m >> i) & 1;
		if(BIT){
			if(trie[now][bit ^ 1] == 0) return tmp;
			now = trie[now][bit ^ 1];
		}else{
			if(trie[now][bit ^ 1] != 0) tmp += cnt[trie[now][bit ^ 1]];
			if(trie[now][bit] == 0) return tmp;
			now = trie[now][bit];
		}
	}
	tmp += cnt[now];
	// cout << cnt[now] << " ";
	return tmp;
}


int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin >> n >> m;
	for(int i = 1;i <= n;i++){
		int x;
		cin >> x;
		insert(sum);
		sum ^= x;
		ans += query(sum);
		// cout << query(sum) << "\n";
	}
	cout << ans;
	return 0;
}

Problem - 282E - Codeforces

先不考虑异或,字典树拥有节点单调性,所以我们找 mex 就找哪边的子树不满,优先选 0 的那边 异或就是将优先级交换,若 flag 当前位为 1 就是优先选 1 的哪边。

由于操作会留下痕迹,所以 flag 异或要累加

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
struct edge{
	ll e,x;
};
ll n,id;
ll a[300005],trie[9100005][2],Size[10000005];

void insert(ll x){
	ll now = 0;
	for(int i = 31;i >= 0;i--){
		ll tmp = (x >> i) & 1;
		if(trie[now][tmp] == 0) trie[now][tmp] = ++id;
		now = trie[now][tmp];
	}
	return ;
}

ll query(ll x){
	ll ans = 0,now = 0;
	for(int i = 31;i >= 0;i--){
		ll y = (x >> i) & 1;
		if(Size[trie[now][y]] == (1 << (i + 1)) - 1){
			now = trie[now][y ^ 1];
			ans += (1 << i);
		}else{
			now = trie[now][y];
		}
	}
	return ans;
}

void dfs(ll x,ll fa = 0){
	Size[x] = 1;
	for(int i = 0;i <= 1;i++){
		if(trie[x][i] != 0){
			dfs(trie[x][i],x);
			Size[x] += Size[trie[x][i]];
		}
	}
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T;
	cin >> n >> T;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
		insert(a[i]);
	}
	dfs(0);
	ll f = 0;
	while(T--){
		ll x;
		cin >> x;
		f ^= x;
		cout << query(f) << "\n";
	}
	return 0;
}

P3375 【模板】KMP - 洛谷

模板

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll Next[1000005];

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void KMP(string s,string t){
	init_Next(t);
	ll i = 0,j = 0;
	while(i < s.size()){
		if(j == t.size() - 1 && s[i] == t[j]){
			cout << i - j + 1 << "\n";
			j = Next[j];
		}
		if(j == -1 || s[i] == t[j]){
			i++;
			j++;
		}else{
			j = Next[j];
		}
	}
	return ;
}

int main(){
	string s,t;
	cin >> s >> t;
	KMP(s,t);
	for(int i = 1;i <= t.size();i++){
		cout << Next[i] << " ";
	}
	return 0;
}

[P3435 POI 2006] OKR-Periods of Words - 洛谷

这题求最大周期,与最小周期对应。 那么求最小公共前缀。 就是一直 j = Next[j] 直到 Next[j] == 0 但是时间会炸…… 所以可以类似于记忆化,将 Next[i] = j 时间就来到了线性

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,ans;
ll Next[1000005];
string s;

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

int main(){
	cin >> n >> s;
	init_Next(s);
	for(int i = 1;i <= n;i++){
		ll tmp = i;
		while(Next[tmp]) tmp = Next[tmp];
		if(Next[i]) Next[i] = tmp;
		ans += i - tmp;
	}
	cout << ans;
	return 0;
}

P4591 [TJOI2018] 碱基序列 - 洛谷

直接 KMP 匹配,拿个 vis 数组标记一下是哪个集合哪一个字符串对应哪个点 然后 DP 。 dp[i][j] 表示匹配到字符串的第 i 项的第 j 个集合的方案数

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll mod = 1e9 + 7;
ll n,m[105];
ll Next[10005],dp[105][10005];
bool vis[105][25][10005];
string s,a[105][25];

void init_Next(string s){
	for(int i = 0;i <= s.size();i++) Next[i] = 1;
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void KMP(string s,string t,ll x,ll y){
	init_Next(t);
	ll i = 0,j = 0;
	while(i < s.size()){
		if(j == t.size() - 1 && s[i] == t[j]){
			vis[x][y][i + 1] = 1;
			j = Next[j];
		}
		if(j == -1 || s[i] == t[j]){
			i++;
			j++;
		}else{
			j = Next[j];
		}
	}
	return ;
}

int main(){
	cin >> n >> s;
	for(int i = 1;i <= n;i++){
		cin >> m[i];
		for(int j = 1;j <= m[i];j++){
			cin >> a[i][j];
		}
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= m[i];j++){
			KMP(s,a[i][j],i,j);
		}
	}
	for(int i = 0;i <= s.size();i++){
		dp[0][i] = 1;
	}
	for(int i = 1;i <= n;i++){
		for(int j = 1;j <= s.size();j++){
			for(int k = 1;k <= m[i];k++){
				if(vis[i][k][j]){
					// cout << i << " " << k << " " << j << "\n";
					dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i][k].size()]) % mod;
				}
			}
		}
	}
	// cout << "----\n";
	// for(int i = 1;i <= n;i++){
	// 	for(int j = 1;j <= s.size();j++){
	// 		cout << dp[i][j] << " ";
	// 	}
	// 	cout << "\n";
	// }
	ll ans = 0;
	for(int i = 1;i <= s.size();i++){
		ans = (ans + dp[n][i]) % mod;
	}
	cout << ans;
	return 0;
}

P4824 [USACO15FEB] Censoring S - 洛谷

我们可以用一个栈,存储已经扫过的字符编号。 每当我们匹配成功,我们就弹出模式串个数个,就可以访问到匹配之前的 Next 值

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll top;
ll Next[1000005],S[1000005],to[1000005];

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void KMP(string s,string t){
	init_Next(t);
	ll i = 0,j = 0;
	while(i < s.size()){
		if(j == -1 || s[i] == t[j]){
			to[i] = j + 1;
			S[++top] = i;
			i++;
			j++;
		}else{
			j = Next[j];
		}
		if(j == t.size()){
			top -= t.size();
			j = to[S[top]];
		}
	}
	return ;
}

int main(){
	string s,t;
	cin >> s >> t;
	KMP(s,t);
	for(int i = 1;i <= top;i++){
		cout << s[S[i]];
	}
	return 0;
}

UVA1328 Period - 洛谷

找最小周期长度是否可以整除这个长度

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n;
ll Next[1000005];
string s;

void init_Next(string s){
	for(int i = 1;i <= s.size();i++) Next[i] = 0;
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void work(){
	init_Next(s);
	for(int i = 0;i < s.size();i++){
		if(i - Next[i + 1] + 1 != i + 1 && i - Next[i + 1] + 1 != 0 && (i + 1) % (i - Next[i + 1] + 1) == 0){
			cout << i + 1 << " " << (i + 1) / (i - Next[i + 1] + 1) << "\n";
		}
	}
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T = 0;
	while(1){
		T++;
		cin >> n;
		if(n == 0) return 0;
		cin >> s;
		cout << "Test case #" << T << "\n";
		work();
		cout << "\n";
	}
	return 0;
}

Substrings - HDU 1238 - Virtual Judge

暴力

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n;
ll Next[105];
string a[105];

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

bool KMP(string s,string t){
	init_Next(t);
	ll i = 0,j = 0;
	while(i < s.size()){
		if(j == t.size() - 1 && s[i] == t[j]){
			return 0;
			j = Next[j];
		}
		if(j == -1 || s[i] == t[j]){
			i++;
			j++;
		}else{
			j = Next[j];
		}
	}
	return 1;
}

bool check(ll l,ll r){
	string tmp = a[1].substr(l,r - l + 1);
	string TMP = tmp;
	reverse(TMP.begin(),TMP.end());
	for(int i = 2;i <= n;i++){
		if(KMP(a[i],tmp) && KMP(a[i],TMP)){
			return 0;
		}
	}
	return 1;
}

void work(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
	}
	ll mx = 0;
	for(int i = 0;i < a[1].size();i++){
		for(int j = i;j < a[1].size();j++){
			if(check(i,j)){
				mx = max(mx,j - i + 1ll);
			} 
		}
	}
	cout << mx << "\n";
	return ;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	ll T;
	cin >> T;
	while(T--) work();
	return 0;
}

Count the string - HDU 3336 - Virtual Judge

DP,看能不能从最长公共前缀转移

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll Next[200005],dp[200005];

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void work(){
	ll n;
	cin >> n;
	string s;
	cin >> s;
	init_Next(s);
	ll ans = 0;
	for(int i = 1;i <= s.size();i++){
		if(Next[i] != -1) dp[i] = dp[Next[i]] + 1;
		dp[i] %= 10007;
		ans += dp[i];
		ans %= 10007;
	}
	cout << ans << "\n";
	return ;
}

int main(){
	ll T;
	cin >> T;
	while(T--) work();
	return 0;
}

P8112 [Cnoi2021] 符文破译 - 洛谷

一边 KMP ,一边跑 DP。 在匹配的时候进行转移

记得在匹配上的时候再转移

Code

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
ll n,m;
ll Next[10000005],dp[10000005];
string a,b;

void init_Next(string s){
	Next[0] = -1;
	ll i = 0,j = -1;
	while(i < s.size()){
		if(j == -1 || s[i] == s[j]){
			i++;
			j++;
			Next[i] = j;
		}else{
			j = Next[j];
		}
	}
	return ;
}

void KMP(string s,string t){
	for(int i = 1;i <= s.size();i++){
		dp[i] = 1e18;
	}
	dp[0] = 0;
	init_Next(t);
	ll i = 0,j = 0;
	while(i < s.size()){
		if(j == t.size() - 1 && s[i] == t[j]){
			dp[i + 1] = min(dp[i + 1],dp[i - j] + 1);
			j = Next[j];
		}
		if(j == -1 || s[i] == t[j]){
			if(j != -1){
				dp[i + 1] = min(dp[i + 1],dp[i - j] + 1);
			}
			i++;
			j++;
			
		}else{
			j = Next[j];
		}
	}
	return ;
}

int main(){
	cin >> n >> m >> a >> b;
	KMP(b,a);
	if(dp[m] < 1e18) cout << dp[m];
	else cout << "Fake";
	return 0;
}
原文