字典树(Trie)
应用
最基础的应用——查找一个字符串是否在字典中出现过
也可以在记录字符串的同时记录每个前缀出现的次数,查询前缀在字典中出现的次数
【模板】字典树
题目描述
给定
一个字符串
输入的字符串大小敏感。例如,字符串 Fusu 和字符串 fusu 不同。
输入格式
本题单测试点内有多组测试数据。
输入的第一行是一个整数,表示数据组数
对于每组数据,格式如下:
第一行是两个整数,分别表示模式串的个数
接下来
接下来
输出格式
按照输入的顺序依次输出各测试数据的答案。
对于每次询问,输出一行一个整数表示答案。
样例 #1
样例输入 #1
3
3 3
fusufusu
fusu
anguei
fusu
anguei
kkksc
5 2
fusu
Fusu
AFakeFusu
afakefusu
fusuisnotfake
Fusu
fusu
1 1
998244353
9样例输出 #1
2
1
0
1
2
1提示
数据规模与约定
对于全部的测试点,保证
说明
std 的 IO 使用的是关闭同步后的 cin/cout,本题不卡常。
题解
for循环外面就是记录每个单词出现次数
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int nex[3000005][120], cnt[3000005]; // nex第二维
void solve() {
int n, m;
cin >> n >> m;
int tot = 0;
auto getnum = [&](char x) { // 字母映射
if (x >= 'A' && x <= 'Z') {
return x - 'A';
} else if (x >= 'a' && x <= 'z') {
return x - 'a' + 26;
} else {
return x - '0' + 52;
}
};
auto insert = [&](string S) { // 插入新字符串
int p = 0;
for (auto i : S) {
int c = getnum(i);
if (!nex[p][c]) { // 如果没有 就添加新节点
nex[p][c] = ++tot;// 开点
}
p = nex[p][c];
cnt[p]++;
}
};
auto query = [&](string S) { // 查找字符串
int p = 0;
for (auto i : S) {
int c = getnum(i);
if (!nex[p][c]) {
return 0;
}
p = nex[p][c];
}
return cnt[p];
};
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
insert(s);
}
while (m--) {
string s;
cin >> s;
cout << query(s) << "\n";
}
for (int i = 0; i <= tot; i++) { // memset会超时
for (int j = 0; j <= 100; j++) {
nex[i][j] = 0;
}
cnt[i]=0;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}P1481 魔族密码
题目背景
风之子刚走进他的考场,就……
花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花)
风之子:我呕……(杀死人的眼神)快说题目!否则……-_-###
题目描述
花花:……咦好冷我们现在要解决的是魔族的密码问题(自我陶醉:搞不好魔族里面还会有人用密码给我和菜虫写情书咧,哦活活,当然是给我的比较多拉*^_^*)。
魔族现在使用一种新型的密码系统。每一个密码都是一个给定的仅包含小写字母的英文单词表,每个单词至少包含
; ; 。
但下面的单词不组成词链:
; 。
现在你要做的就是在一个给定的单词表中取出一些词,组成最长的词链,就是包含单词数最多的词链。将它的单词数统计出来,就得到密码了。
风之子:密码就是最长词链所包括的单词数阿……
输入格式
这些文件的格式是,第一行为单词表中的单词数
输出格式
输出共一行,一个整数,表示密码。
输入输出样例 #1
输入 #1
5
i
int
integer
intern
internet输出 #1
4题解
记录前缀次数,查找一个单词包含最多前缀次数
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
int n;
cin >> n;
int tot = 0, ans = 0;
vector<vector<int>> nex(n * 75, vector<int>(70));
vector<int> cnt(n * 75);
auto insert = [&](string S) {
int p = 0, mx = 0;
for (auto i : S) {
int c = i - 'a' + 1;
if (!nex[p][c]) {
nex[p][c] = ++tot;
}
p = nex[p][c];
mx += cnt[p];
}
ans = max(ans, mx + 1); //+1加上自己本身
cnt[p]++;
};
for (int i = 0; i < n; i++) {
string s;
cin >> s;
insert(s);
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}P2922 [USACO08DEC] Secret Message G
题目描述
Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret binary messages to each other.
Ever the clever counterspy, Farmer John has intercepted the first
He has compiled a list of
For each codeword
The total number of bits in the input (i.e., the sum of the
贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.
信息是二进制的,共有
对于每条暗号
在输入文件中,位的总数(即
输入格式
Line
Lines 0's and 1's.
Lines 0's and 1's.
输出格式
Lines
输入输出样例 #1
输入 #1
4 5
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1输出 #1
1
3
1
1
2说明/提示
Four messages; five codewords.
The intercepted messages start with 010, 1, 100, and 110.
The possible codewords start with 0, 1, 01, 01001, and 11.
0 matches only 010: 1 match
1 matches 1, 100, and 110: 3 matches
01 matches only 010: 1 match
01001 matches 010: 1 match
11 matches 1 and 110: 2 matches
题解
典型的字典树统计题。
首先是读入。这道题全部采用2进制,也就是说只有0和1两个字符/数字,那么我们直接把它们当成字符用好了。
然后建树,也是一般的字典树建树法,只是把字母换成了0和1而已,这里不多讲。
一边建树,一边在这串数列中走过的路径中的sum+1,sum代表有sum个单词(?)经过这个节点。
当一个单词(?)插完以后,在当前节点(即该单词的最后一个数字)的end上+1,end代表有end个单词(?)在这个节点终结。
注意可能会有重复的信息,也就是说可能会有多条信息在同一个节点终结,所以这里的end是数字而不是布尔型。
然后读入待查询的信息,就像普通字典树查询一样地往下走,一边走一边把沿途的end值加起来。
循环结束,有两种情况:一是该信息全部走完,二是再往下走没有与该信息相符的节点了。
第一种情况,当前的答案要减去当前节点的end值再加上当前节点的sum值(想一想,为什么)。
第二种情况,直接输出答案。
显然比待查询的信息长的信息,如果与待查询信息有相同前缀的话,一定会经过待查询信息终结的节点。
如果待查询信息无法终结,说明没有比该信息长且前缀是该信息的信息,所以不能加上sum。
如果信息终结,那么当前节点的sum值所包含的信息一定与其有相同前缀,但sum所包含的信息有可能刚好在该节点终结,
所以要减去end值。
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int trie[500005][3], sum[500005], cnt[500005];
void solve() {
int M, N;
cin >> M >> N;
int tot = 0;
memset(trie, -1, sizeof(trie)); // 待统计的字符为01 初始化为-1或其他
auto add = [&](vector<int> a) {
int p = 0;
for (auto i : a) {
if (trie[p][i] == -1) {
trie[p][i] = ++tot;
}
p = trie[p][i];
sum[p]++; // 统计经过该节点的字符串的个数
}
cnt[p]++; // 统计字符串以p结尾的个数
};
auto query = [&](vector<int> a) {
int p = 0;
int res = 0; // 统计有几个字符串是a的前缀
for (auto i : a) {
if (trie[p][i] == -1) { // 查不下去说明没有比当前字符串长的
return res; // 直接返回有几个字符串是a的前缀
}
p = trie[p][i];
res += cnt[p]; // 加上以当前节点为结束的个数
}
return res - cnt[p] + sum[p];
// 要加上比待查询字符串长的字符串 sum包含自身所以要减去cnt
};
for (int i = 0; i < M; i++) {
int k;
cin >> k;
vector<int> a;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
a.push_back(x);
}
add(a);
}
while (N--) {
int k;
cin >> k;
vector<int> a;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
a.push_back(x);
}
cout << query(a) << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}P10470 前缀统计
题目描述
给定
输入字符串的总长度不超过
输入格式
第一行输入两个整数
接下来
接下来
输出格式
对于每个询问,输出一个整数表示答案。
每个答案占一行。
输入输出样例 #1
输入 #1
3 2
ab
bc
abc
abc
efg输出 #1
2
0说明/提示
数据范围满足
题解
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int trie[1000005][120], cnt[1000005];
void solve() {
int n, m;
cin >> n >> m;
int tot = 0;
auto add = [&](string s) {
int p = 0;
for (auto i : s) {
int c = i - 'a';
if (!trie[p][c]) {
trie[p][c] = ++tot;
}
p = trie[p][c];
}
cnt[p]++;
};
auto query = [&](string s) {
int p = 0;
int ans = 0;
for (auto i : s) {
int c = i - 'a';
if (!trie[p][c]) {
return ans;
}
p = trie[p][c];
ans += cnt[p];
}
return ans;
};
for (int i = 0; i < n; i++) {
string s;
cin >> s;
add(s);
}
while (m--) {
string s;
cin >> s;
cout << query(s) << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--) {
solve();
}
return 0;
}