代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e4 + 10;
string st[N], s;
int n, q;
int get(char c) {
int idx;
if(c >= 'a' && c <= 'z') idx = c - 'a';
else if(c == '.') idx = 36;
else idx = 26 + c - '0';
return idx + 1;
}
int main()
{
scanf("%d", &n);
unordered_map<LL, int> mp, res;
for(int i = 0; i < n; i++) {
cin >> st[i];
int len = st[i].size();
unordered_map<LL, bool> vis;
for(int j = 0; j < len; j++) {
LL idx = 0, t = 1;
for(int k = j; k < len; k++) {
idx += t * get(st[i][k]);
t *= 38;
if(!vis[idx]) {
vis[idx] = true;
mp[idx]++;
res[idx] = i;
}
}
}
}
scanf("%d", &q);
LL idx, t;
while(q--) {
cin >> s;
idx = 0, t = 1;
for(auto& c : s) {
idx += t * get(c);
t *= 38;
}
if(mp[idx] == 0) puts("0 -");
else {
cout << mp[idx] << ' ' << st[res[idx]] << endl;
}
}
return 0;
}
执行时间:2s
所需空间:18000kb