BerOS File Suggestion
题解:这道题说实话有点想多了,还考虑用 t r i e trie trie树去做(虽然有神犇这样做的),可惜蒻根本不会 t r i e trie trie树。实际上用 m a p map map统计子串出现次数并记录子串的原串就好了。
代码
#include<bits/stdc++.h>
using namespace std;
map<string,int> vis, cnt;
map<string,string> str;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n;
ios::sync_with_stdio(false);
cin>>n;
string s;
for(int i = 0; i < n; ++i){
cin>>s;
vis.clear();
string ret;
for(int i = 0; i < s.length(); ++i){
ret = "";
for(int j = i; j < s.length(); ++j){
ret += s[j];
if(vis[ret] == 0) {
vis[ret] = 1;
cnt[ret]++;
str[ret] = s;
// cout<<ret<<endl;
}
}
}
}
int q;
cin>>q;
string ans;
while(q--){
cin>>ans;
if(cnt[ans] == 0) {
cout<<"0 -"<<endl;
}else{
cout<<cnt[ans]<<' '<<str[ans]<<endl;;
}
}
return 0;
}