题目解析+总结
P8306 【模板】字典树 / Trie
题目解析
字典树可以看成一本字典,一般应用在给定一些字符串,需要求出字符串里面有多少字符串的前缀是每次询问中给出的字符串。
我们先举一个例子,现在有三个如下所示的字符串:
abc
abd
bcd我们发现字符串和字符串的ab可以进行合并,所以我们可以转化成这样:
a - b - c
\
d
b - c - d这就是字典树的原理,把这条路径上的字符串中如果路径相同就合并。
要是我们需要实现字典树的话,我们可以建造一个二维数组:
int G[N][C];
//N:当前字典树上的节点
//C:下一步的字符
//值:下一步走到的儿子节点编号,0代表没有那么我们可以这样建图:

这样空间复杂度只有。要是不用memset并且开int,GB绝对没有问题。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pait pair<int,int>
const int N = 3e6+7;
const int Mod = 998244353;
int n,q,G[N][65];
int cnt[N],tot = 0;
int get(char c){
if(c >= 'A' && c <= 'Z') return c - 'A';
if(c >= 'a' && c <= 'z') return c - 'a' + 26;
return c - '0' + 52;
}
int query(string s){
int u = 0;
for(int j=0;j<(int)(s.size());++j){
int c = get(s[j]);
if(G[u][c] == 0) return 0;
u = G[u][c];
}
return cnt[u];
}
void insert(string s){
int len = s.size();
int u = 0;
for(int j=0;j<len;++j){
int c = get(s[j]);
if(G[u][c] == 0) ++tot,G[u][c] = tot;
u = G[u][c],cnt[u]++;
}
}
void solve(){
cin>>n>>q;
for(int i=0;i<=tot;++i){
cnt[i] = 0;
for(int j=0;j<65;++j) G[i][j] = 0;
}
tot = 0;
for(int i=1;i<=n;++i){
string s; cin>>s;
insert(s);
}
while(q--){
string t; cin>>t;
cout<<query(t)<<endl;
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t; cin>>t;
while(t--) solve();
return 0;
}P10470 前缀统计
题目解析
这里也是一道模版题,只不过反过来了——求出字符串里面有多少字符串是每次询问中给出的字符串的前缀。
我们观察可以发现,树中满足条件的字符串的末尾都被标记了。

那么求这个问题也就十分简单:统计被标记的数量就可以了。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pait pair<int,int>
const int N = 1e6+7;
const int Mod = 998244353;
int n,q,G[N][65];
int cnt[N],tot = 0;
int get(char c){
if(c >= 'A' && c <= 'Z') return c - 'A';
if(c >= 'a' && c <= 'z') return c - 'a' + 26;
return c - '0' + 52;
}
int query(string s){
int u = 0,ans = 0;
for(int j=0;j<(int)(s.size());++j){
int c = get(s[j]);
if(G[u][c] == 0) break;
u = G[u][c],ans += cnt[u];
}
return ans;
}
void insert(string s){
int len = s.size();
int u = 0;
for(int j=0;j<len;++j){
int c = get(s[j]);
if(G[u][c] == 0) ++tot,G[u][c] = tot;
u = G[u][c];
}
cnt[u]++;
}
void solve(){
cin>>n>>q;
for(int i=0;i<=tot;++i){
cnt[i] = 0;
for(int j=0;j<65;++j) G[i][j] = 0;
}
tot = 0;
for(int i=1;i<=n;++i){
string s; cin>>s;
insert(s);
}
while(q--){
string t; cin>>t;
int top = query(t);
cout<<top<<endl;
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}P2580 于是他错误的点名开始了
题目解析
实际上这道题只需要map就可以解决了。但是我们先继续使用字典树的写法。
实际上就是第一题的模版题,要是字符串的位置已经被标记了,那么就是合法的,否则就不合法。这是第一种情况。
第二种情况我们直接map去重,要是这个字符串已经存在了,说明重复了,否则就没有重复。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pait pair<int,int>
const int N = 1e6+7;
const int Mod = 998244353;
int n,q,G[N][65];
int cnt[N],tot = 0;
int get(char c){
if(c >= 'A' && c <= 'Z') return c - 'A';
if(c >= 'a' && c <= 'z') return c - 'a' + 26;
return c - '0' + 52;
}
int query(string s){
int u = 0;
for(int j=0;j<(int)(s.size());++j){
int c = get(s[j]);
if(G[u][c] == 0) return 0;
u = G[u][c];
}
return cnt[u];
}
void insert(string s){
int len = s.size();
int u = 0;
for(int j=0;j<len;++j){
int c = get(s[j]);
if(G[u][c] == 0) ++tot,G[u][c] = tot;
u = G[u][c];
}
cnt[u]++;
}
void solve(){
cin>>n;
for(int i=0;i<=tot;++i){
cnt[i] = 0;
for(int j=0;j<65;++j) G[i][j] = 0;
}
tot = 0;
for(int i=1;i<=n;++i){
string s; cin>>s;
insert(s);
}
cin>>q;
map<string,bool>op;
while(q--){
string t; cin>>t;
int top = query(t);
if(top == 1){
if(op[t] == 1) cout<<"REPEAT\n";
else cout<<"OK\n";
op[t] = 1;
}
else cout<<"WRONG\n";
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
P4683 [IOI 2008] Type Printer
题目解析
这道题上了大大大大难度了,但是好像有没有,因为只是一个小小的DFS模版,但是也加了点贪心。就是拼好题
我们观察样例发现:在打印最后一个单词后直接结束了。所以我们要让长度最长的字符串当做最后一次遍历。
我们遍历的时候要是是从下往上就输出字母,要是从下往上就输出减号,要是到达末尾就直接输出打印符号P。
要是我们处理上面那种情况,我们可以现将那个字符串进行标记,然在遍历的时候要是遇到了标记就直接进行记录,也就是说就一去不复返了。
因为输出前还需要输出操作次数,所以我们就用一个队列进行统计,就可以了。
实际上这道题看似简单,实际上一点都不难。
AC代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pait pair<int,int>
const int N = 5e5+7;
const int Mod = 998244353;
int n,q,G[N][30];
int cnt[N],tot = 0;
bool vis[N];
queue<char>op;
void insert(string s){
int len = s.size();
int u = 0;
for(int j=0;j<len;++j){
int c = s[j] - 'a';
if(G[u][c] == 0) G[u][c] = ++tot;
u = G[u][c];
}
cnt[u]++;
}
void dfs(int cur){
if(cnt[cur]) op.push('P');
int pos = -1;
for(int i=0;i<26;++i){
int top = G[cur][i];
if(vis[top]) pos = i;
else if(top){
op.push(i + 'a');
dfs(top);
}
}
if(pos == -1) op.push('-');
else if(pos != -1){
op.push(pos + 'a');
dfs(G[cur][pos]);
}
}
void set9(string s){
int u = 0,len = s.size();
for(int i=0;i<len;++i){
int c = s[i] - 'a';
u = G[u][c];
vis[u] = 1;
}
}
void solve(){
cin>>n;
for(int i=0;i<=tot;++i){
cnt[i] = 0;
for(int j=0;j<65;++j) G[i][j] = 0;
}
tot = 0;
int maxx = 0;
string x = "";
for(int i=1;i<=n;++i){
string s; cin>>s;
insert(s); int len = s.size();
if(len > maxx) maxx = len,x = s;
}
set9(x),dfs(0);
cout<<op.size() - 1<<endl;
while(op.size() - 1 > 0){
cout<<op.front()<<endl;
op.pop();
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
homework~
P2922 [USACO08DEC] Secret Message G
AC代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pait pair<int,int>
const int N = 3e6+7;
const int Mod = 998244353;
int n,q,G[N][65];
int cnt[N],cnt2[N],tot = 0;
int get(char c){
if(c >= 'A' && c <= 'Z') return c - 'A';
if(c >= 'a' && c <= 'z') return c - 'a' + 26;
return c - '0' + 52;
}
int query(string s){
int u = 0,ans = 0;
for(int j=0;j<(int)(s.size());++j){
int c = get(s[j]);
if(G[u][c] == 0) return ans;
u = G[u][c],ans += cnt2[u];
}
return cnt[u] + ans - cnt2[u];
}
void insert(string s){
int len = s.size();
int u = 0;
for(int j=0;j<len;++j){
int c = get(s[j]);
if(G[u][c] == 0) ++tot,G[u][c] = tot;
u = G[u][c],cnt[u]++;
}
cnt2[u]++;
}
void solve(){
cin>>n>>q;
for(int i=0;i<=tot;++i){
cnt[i] = 0;
for(int j=0;j<65;++j) G[i][j] = 0;
}
tot = 0;
for(int i=1;i<=n;++i){
int x; cin>>x;
string s = "";
for(int j=1;j<=x;++j){
int a; cin>>a;
s += a + '0';
}
insert(s);
}
while(q--){
string s = "";
int x; cin>>x;
for(int j=1;j<=x;++j){
int a; cin>>a;
s += a + '0';
}
cout<<query(s)<<endl;
}
}
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}