๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1302๋ฒˆ - ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ

by dkswnkk 2021. 11. 4.
 

1302๋ฒˆ: ๋ฒ ์ŠคํŠธ์…€๋Ÿฌ

์ฒซ์งธ ์ค„์— ์˜ค๋Š˜ ํ•˜๋ฃจ ๋™์•ˆ ํŒ”๋ฆฐ ์ฑ…์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ฐ’์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ฑ…์˜ ์ œ๋ชฉ์ด ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜จ๋‹ค. ์ฑ…์˜ ์ œ๋ชฉ์˜ ๊ธธ์ด๋Š” 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๊ณ 

www.acmicpc.net

#include <iostream>
#include <map>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    map<string, int>m;
    int N; cin >> N;
    while (N--) {
        string s; cin >> s;
        m[s]++;        
    }
    int ans = 0;
    for (auto i = m.begin(); i != m.end(); i++) {
        ans = max(ans, i->second);    //๊ฐ€์žฅ ํฐ ๊ฐ’ ์ฐพ๊ธฐ
    }
    for (auto k = m.begin(); k != m.end(); k++) {
        if (ans == k->second) {    //๊ฐ€์žฅ ํฐ ๊ฐ’์ด ์žˆ๋Š” ์œ„์น˜ ์ฐพ๊ธฐ
            cout << k->first;
            return 0;
        }
    }
}

๋Œ“๊ธ€