0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00
Baoshuo Ren 2021-10-03 08:35:29 +08:00 committed by Baoshuo Ren
parent 52d09159cf
commit a608ebd2c0
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

51
AcWing/142/142.cpp Normal file
View 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;
}