題目大意: 就處理字串,然後做排名。
思路: 用map。並且用優先隊列。最後再將答案輸出。字串處理。遇到字元則延長。否則丟入map,水題。純實作。
代碼:
#include <bits/stdc++.h>
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define louisfghbvc int t; cin >> t; while(t--)
using namespace std;
void solve(){
string str;
map<string, int> mp;
while(getline(cin, str)){
str += '$';
string tmp = "";
for(char c: str){
if(isalpha(c)) c = tolower(c), tmp += c;
else if(tmp.size()) mp[tmp]++, tmp = "";
}
}
priority_queue<pair<int, string>> pq;
for(auto &[a, b]: mp){
pq.push({-b, a});
if(pq.size() > 3) pq.pop();
}
deque<pair<string, int>> ans;
while(pq.size()){
auto [a, b] = pq.top(); pq.pop();
ans.push_front({b, -a});
}
for(auto &[a, b]: ans)
cout << a << " = " << b << "\n";
}
int main()
{
//Fast;
//louisfghbvc
solve();
return 0;
}