close

題目大意:  就處理字串,然後做排名。

思路:  用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;
}

arrow
arrow
    創作者介紹
    創作者 尾玉 的頭像
    尾玉

    louisfghbvc的部落格

    尾玉 發表在 痞客邦 留言(0) 人氣()