跳到正文

个人记录

8.23总结

Daniel_Chou_hn约 9 分钟
原文

这是 2026年8月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月23日
收录
2026年8月24日
更新
2026年11月21日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

字典树

判断一个字符串AA是否为另一个字符串BB的前缀,如何判断呢?

第一种方法

C++
if(b.find(a)==0)

O(N)O(N),不怎么快速,不怎么便捷

第二种办法

我们可以把两个字符串建树,比如字符串AA为ABCDE,BB为ABCDEFG,树如下

代码
   A
   |
   B
   |
   C
   |
   D
   |
   E //A截止点
   |
   F
   |
   G//B截止点

判断AA是否为BB的前缀,只需判断AA的截止点是否在通往BB的截止点的路径上就行了

这,就是字典树

1.【模板】字典树

字典树模板

C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e6+7;
int node[N][65];
int cnt[N];
int tot=0;
int getas(char c){
	if(c>='a' and c<='z')return c-'a';
	if(c>='A' and c<='Z')return c-'A'+26;
	return c-'0'+52;
}
void insert(string s){
	int u=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			++tot;
			node[u][c]=tot;
		}
		u=node[u][c];
		cnt[u]++;
	}
	return;
}
int query(string s){
	int u=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			return 0;
		}
		u=node[u][c];
	}
	return cnt[u];
}
int main(){
	ios::sync_with_stdio(0),cin.tie(nullptr);
	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		for(int i=0;i<=tot;i++){
			for(int j=0;j<=61;j++){
				node[i][j]=0;
			}
			cnt[i]=0;
		}
		tot=0;
		for(int i=1;i<=n;i++){
			string s;
			cin>>s;
			insert(s);
			
		}
		while(m--){
			string x;
			cin>>x;
			cout<<query(x)<<'\n';
		}
	}
	return 0;
}

2.前缀统计

做一点小小的修改

在每个字符串截止点做标记,同时在获取时统计每一个遇到的截止点

C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e6+7;
int node[N][65];
int cnt[N];
int vis[N];
int tot=0;
int getas(char c){
	if(c>='a' and c<='z')return c-'a';
	if(c>='A' and c<='Z')return c-'A'+26;
	return c-'0'+52;
}
void insert(string s){
	int u=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			++tot;
			node[u][c]=tot;
		}
		u=node[u][c];
		cnt[u]++;
	}
	vis[u]++;
	return;
}
int query(string s){
	int u=0;int ans=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			return ans;
		}
		
		u=node[u][c];
		ans+=vis[u];
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(nullptr);
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		insert(s);
		
	}
	while(m--){
		string x;
		cin>>x;
		cout<<query(x)<<'\n';
	}
	return 0;
}

3.错误的点名

这里可以用字典树代替map来计算

C++
#include<bits/stdc++.h>
using namespace std;
const int N=3e6+7;
int node[N][65];
int cnt[N];
int exist[N];
int tot=0;
int getas(char c){
	if(c>='a' and c<='z')return c-'a';
	if(c>='A' and c<='Z')return c-'A'+26;
	return c-'0'+52;
}
void insert(string s){
	int u=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			++tot;
			node[u][c]=tot;
		}
		u=node[u][c];
	}
	exist[u]++;
	return;
}
int query(string s){
	int u=0;int ans=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			return 0;
		}
		
		u=node[u][c];
	}
	return exist[u];
}
int main(){
	ios::sync_with_stdio(0),cin.tie(nullptr);
	int n,m;
	cin>>n;
	for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		insert(s);
		
	}
	cin>>m;
	while(m--){
		string x;
		cin>>x;
		int ans=query(x);
		if(ans==1){
			cout<<"OK"<<'\n';
			insert(x);
		}
		else if(ans==2){cout<<"REPEAT"<<'\n';}else{cout<<"WRONG\n";}
	}
	return 0;
}

4.打印机

可以用 DFS 完成

偷截一张老师的图片 可能有点多了

总之你看上面↑可以看到DFS的流程

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+7;
int node[N][26];
int cnt[N];
bool vis[N];
int tot=0;
int n,mmax=0;
vector<char>ans;
int getas(char c){
	return c-'a';
}
void insert(string s){
	int u=0;
	for(int i=0;i<s.size();i++){
		int c=getas(s[i]);
		if(node[u][c]==0){
			++tot;
			node[u][c]=tot;
		}
		u=node[u][c];
	}
	cnt[u]=1;
	return;
}
void set_tag(string s){
	int u=0;
	int len=s.size();
	for(int i=0;i<len;i++){
		int c=s[i]-'a';
		u=node[u][c];
		vis[u]=1;
	}
	return;
	
}
void dfs(int u){
	if(cnt[u])ans.push_back('P');
	int pos=-1;
	for(int i=0;i<26;i++){
		int nxt=node[u][i];
		if(vis[nxt]){
			pos=i;
		}
		else if(nxt){
			ans.push_back(i+'a');
			dfs(nxt);
		}
	}
	if(pos==-1)ans.push_back('-');
	if(pos!=-1){
		ans.push_back(pos+'a');
		dfs(node[u][pos]);
	}
}
int main(){
	ios::sync_with_stdio(0),cin.tie(nullptr);
	cin>>n;
	string s;
	for(int i=1;i<=n;i++){
		string x;
		cin>>x;
		int len=x.size();
		insert(x);
		if(len>mmax){
			mmax=len;
			s=x;
		}
	}
	set_tag(s);
	dfs(0);
	cout<<ans.size()-1<<'\n';
	for(int i=0;i<ans.size()-1;i++)cout<<ans[i]<<'\n';
	return 0;
}