题目链接:周赛46 第三题 查询字符串
思路:
不得不说,本周的周赛都比较简单,第三题和第二题不太符合它的难度定级。
这题我们可以用哈希表unordered_map来解决,做法很简单,只需把每个字符串的字串全部遍历一遍,然后给哈希表对应的值增一就行了,需要注意的只有一点,每个字符串的字串可能会有重复,我们需要加一个去重操作,如此一来,一个困难级的问题就解决了。
代码如下:
#include<bits/stdc++.h>
using namespace std;
struct si{
string s;
int yf;
};
unordered_map<string,si> q;
string p[10005];
int main(){
int n,m;
cin>>n;
for(int l=0;l<n;l++){
string s;
cin>>s;
p[l]=s;
unordered_map<string,bool> p;
for(int i=1;i<=s.length();i++){
for(int j=0;j<s.length();j++){
string c=s.substr(j,i);
if(!p.count(c)) {
q[c].yf++;
q[c].s=s;
p[c]=1;
}
}
}
}
cin>>m;
while(m--){
string s;
cin>>s;
if(q[s].yf==0) cout<<"0 -"<<endl;
else cout<<q[s].yf<<" "<<q[s].s<<endl;
}
}