cf1070H

这道题是一道暴力破解题,不过刚开始时并没有做对,一直xjb写,结果超时,其实注意审题会发现查询的数量先让更频繁,所以应该对输入的每个字符串做处理。
再用两个map保存一下,注意不要数重复的子串就行了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31


using namespace std;
map<string, int> ans;
map<string, string> t;
int ()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin>>n;
string s;
for(int i = 0; i < n; i++){
cin>>s;
for(int j = 1; j <= s.length(); j++){
for(int k = 0; k+j <= s.length(); k++){
string tmp = s.substr(k, j);
if(t[tmp] != s)ans[tmp]++;
t[tmp] = s;
}
}
}
cin>>q;
while(q--){
string tmp;
cin>>tmp;
if(ans[tmp] == 0){cout<<"0 -"<<endl;}
else cout<<ans[tmp]<<" "<<t[tmp]<<endl;
}
return 0;
}