显示代码纯文本
#include <bits/stdc++.h>
using ull = unsigned long long;
const ull P = 13331, SOS = 3376795643;
int n;
std::string pep[110];
char s[110][210];
ull h[110][210], p[210];
std::map<int, std::vector<int>> mp;
ull get(int l, int r, int x) {
return h[x][r] - h[x][l - 1] * p[r - l + 1];
}
int main() {
freopen("noi_online2020_save.in", "r", stdin);
freopen("noi_online2020_save.out", "w", stdout);
std::cin >> n;
for (int i = 1; i <= n; ++ i) std::cin >> pep[i] >> s[i] + 1;
p[0] = 1;
for (int i = 1; i <= n; ++ i) {
int len = strlen(s[i] + 1);
for (int j = 1; j <= len; ++ j) {
h[i][j] = h[i][j - 1] * P + (s[i][j] - 'a' + 1);
p[j] = p[j-1] * P;
}
}
int mx = 0;
for (int i = 1; i <= n; ++ i) {
int cnt = 0;
int len = strlen(s[i] + 1);
for (int j = 1; j <= len - 2; ++ j)
if (get(j, j + 2, i) == SOS) ++ cnt;
mp[cnt].emplace_back(i);
mx = std::max(mx, cnt);
}
for (auto i : mp[mx])
std::cout << pep[i] << ' ';
std::cout << '\n' << mx;
return 0;
}