0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 16:36:28 +00:00

#1851. 【Gym104090K】Master of Both

https://sjzezoj.com/submission/67458
This commit is contained in:
Baoshuo Ren 2023-01-28 17:13:07 +08:00
parent 604ba9a0c3
commit 83d14980b6
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

69
S2OJ/1851/1851.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e6 + 5;
int n, q, cnt, tr[N][26], siz[N][26];
long long f[26][26], res;
void insert(std::string s) {
int cur = 0;
for (int i = 0; i < s.size(); i++) {
int x = s[i] - 'a';
if (!tr[cur][x]) {
tr[cur][x] = ++cnt;
}
for (int j = 0; j < 26; j++) {
if (!tr[cur][j] || j == x) continue;
f[x][j] += siz[cur][j];
}
siz[cur][x]++;
cur = tr[cur][x];
}
for (int i = 0; i < 26; i++) {
if (!tr[cur][i]) continue;
res += siz[cur][i];
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
std::string s;
cin >> s;
insert(s);
}
while (q--) {
long long ans = 0;
std::string t;
cin >> t;
for (int i = 0; i < 26; i++) {
for (int j = i + 1; j < 26; j++) {
ans += f[t[i] - 'a'][t[j] - 'a'];
}
}
cout << ans + res << endl;
}
return 0;
}