mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-08 15:58:48 +00:00
142. 前缀统计
https://www.acwing.com/problem/content/submission/code_detail/8017084/
This commit is contained in:
parent
52d09159cf
commit
a608ebd2c0
51
AcWing/142/142.cpp
Normal file
51
AcWing/142/142.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int n, m, ans;
|
||||||
|
string s;
|
||||||
|
|
||||||
|
struct node {
|
||||||
|
int size;
|
||||||
|
node* next[26];
|
||||||
|
|
||||||
|
~node() {
|
||||||
|
for (auto& i : next) {
|
||||||
|
delete i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void insert(node* root, string s) {
|
||||||
|
if (s.empty()) {
|
||||||
|
root->size++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (root->next[s[0] - 'a'] == nullptr) root->next[s[0] - 'a'] = new node();
|
||||||
|
insert(root->next[s[0] - 'a'], s.substr(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int query(node* root, string s) {
|
||||||
|
if (s.empty()) return root->size;
|
||||||
|
if (root->next[s[0] - 'a'] == nullptr) return 0;
|
||||||
|
return query(root->next[s[0] - 'a'], s.substr(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
node* root = new node();
|
||||||
|
cin >> n >> m;
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> s;
|
||||||
|
insert(root, s);
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= m; i++) {
|
||||||
|
ans = 0;
|
||||||
|
cin >> s;
|
||||||
|
for (int j = 1; j <= s.size(); j++) {
|
||||||
|
ans += query(root, s.substr(0, j));
|
||||||
|
}
|
||||||
|
cout << ans << endl;
|
||||||
|
}
|
||||||
|
delete root;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user