bool cmp(pair<string, int> a, pair<string, int> b){
return a.second>b.second;
}
int main(){
int n, m, k;
cin>>n>>m>>k;
getchar();
map<string, map<string, int> > Map;
//map<前缀,map<后缀,出现的次数> > Map;
//每一个前缀对应一个map
//这个map将【后缀】对应于它出现的【次数】,并将所有键值对按【后缀】的顺序排列
string str;
for(int i=0;i<n;i++){
getline(cin, str);
for(int j=0;j<str.size();j++){
if(str[j]==' '){
string prefix = str.substr(0,j);
//cout<<prefix<<" ";
for(int k=j+1;k<=str.size();k++){
if((k<str.size()&&str[k]==' ')||k==str.size()){
string next_word = str.substr(j+1,k-j-1);
//cout<<next_word;
Map[prefix][next_word]++;
//cout<<" "<<Map[prefix][next_word]<<endl;
break;
}
}
}
}
}
string query;
for(int i=0;i<m;i++){
getline(cin, query);
vector<pair<string, int>> vec;
map<string,int>::iterator iter;
//Map[query]是一个map
//将Map[query]中的键值对转移到vector容器中(为了排序)
for(iter=Map[query].begin();iter!=Map[query].end();iter++){
vec.push_back(make_pair(iter->first, iter->second));
}
stable_sort(vec.begin(), vec.end(), cmp);
for(int j=0;j<vec.size();j++){
if(j<k){
cout<<vec[j].first<<" "<<endl;
}
else
break;
}
}
return 0;
}
/*
3 2 3
the best office software feishu
the best bussiness software lark
the best bussiness software feishu
the best bussiness software
the best
*/
智能补全【按词频递减,字典序递增输出topK】
最新推荐文章于 2024-07-17 20:43:45 发布