new file: P8306.cpp

This commit is contained in:
hanyixuanten committed 2026-10-28 10:06:33 +00:00
1 parent 68c1db108d
commit 68db99a45c
2 files changed
+136

No files matched your search

+65
View File
@@ -0,0 +1,65 @@
#include <bits/stdc++.h>
using namespace std;
int t;
int n, q;
struct node{
vector<node*> child;
char c;
int cnt = 0;
int childsize = 0;
};
char s[3000005];
int len;
void add(node *now, int i) {
now -> childsize++;
if(i == len) {
now -> cnt++;
return;
}
// if(now -> c == ' ') {
bool flag = 0;
for(auto c : now -> child) {
if(c -> c == s[i]) {
add(c, i + 1);
flag = 1;
break;
}
}
if(!flag) {
node *nc = new node;
nc -> c = s[i];
now->child.push_back(nc);
add(nc, i + 1);
}
// }
}
int query(node *now, int i) {
if(i == len) {
return now -> childsize;
}
for(auto c : now -> child) {
if(c -> c == s[i]) {
return query(c, i + 1);
}
}
return 0;
}
int main() {
scanf("%d", &t);
while(t--) {
node *root = new node;
// root -> c=' ';
scanf("%d%d", &n, &q);
for(int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
len = strlen(s + 1) + 1;
add(root, 1);
}
for(int i = 1; i <= q; ++i) {
scanf("%s", s + 1);
len = strlen(s + 1) + 1;
printf("%d\n", query(root, 1));
}
}
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
# P8306 【模板】字典树 / Trie
## 题目描述
给定 $n$ 个模式串 $s_1, s_2, \dots, s_n$ 和 $q$ 次询问,每次询问给定一个文本串 $t_i$,请回答 $s_1 \sim s_n$ 中有多少个字符串 $s_j$ 满足 $t_i$ 是 $s_j$ 的**前缀**。
一个字符串 $t$ 是 $s$ 的前缀当且仅当从 $s$ 的末尾删去若干个(可以为 $0$ 个)连续的字符后与 $t$ 相同。
输入的字符串大小写敏感。例如,字符串 `Fusu` 和字符串 `fusu` 不同。
## 输入格式
**本题单测试点内有多组测试数据**。
输入的第一行是一个整数,表示数据组数 $T$。
对于每组数据,格式如下:
第一行是两个整数,分别表示模式串的个数 $n$ 和询问的个数 $q$。
接下来 $n$ 行,每行一个字符串,表示一个模式串。
接下来 $q$ 行,每行一个字符串,表示一次询问。
## 输出格式
按照输入的顺序依次输出各测试数据的答案。
对于每次询问,输出一行一个整数表示答案。
## 输入输出样例 #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
```
## 说明/提示
### 数据规模与约定
对于全部的测试点,保证 $1 \leq T, n, q\leq 10^5$,且输入字符串的总长度不超过 $3 \times 10^6$。输入的字符串只含大小写字母和数字,且不含空串。
### 说明
std 的 IO 使用的是关闭同步后的 cin/cout,本题不卡常。