안녕하세요. 오늘은 인기도를 측정할 거예요.

문제

https://www.acmicpc.net/problem/25325

아이디어

이름이 입력으로 들어올때마다 바로바로 개수를 카운트 할 수 있도록 맵에 저장을 합시다.
이때 입력은 개수제한 없이 들어오므로 while문 안에 cin을 넣어주면 됩니다.
이때 출력할 때에는 또 정렬을 해주어야하기 때문에 벡터에 넣어줄 겁니다.

소스코드

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std;

ll cmp(pair <string, ll> A, pair <string, ll> B)
{
    if (A.second != B.second) return A.second > B.second;
    return A.first < B.first;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    map <string, ll> names;
    ll N, i;
    string s;

    cin >> N;
    for (i = 1; i <= N; i++)
    {
        cin >> s;
        names[s] = 0;
    }
    while (cin >> s)
        names[s]++;

    vector <pair <string, ll> > v;
    for (auto name : names)
        v.push_back(name);
    sort(v.begin(), v.end(), cmp);

    for (auto name : v)
        cout << name.first << ' ' << name.second << "\n";
}


감사합니다.

0개의 댓글