0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:48:48 +00:00

K - Master of Both

https://codeforces.com/gym/104090/submission/190952260
This commit is contained in:
Baoshuo Ren 2023-01-28 17:12:11 +08:00
parent 14a16641ca
commit 604ba9a0c3
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

70
Gym/104090/K/K.cpp Normal file
View File

@ -0,0 +1,70 @@
#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;
}