fork download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. int main() {
  6. string str;
  7. cin >> str;
  8.  
  9. map<char, int> freq; // 문자별 등장 횟수 저장
  10.  
  11. // 문자열의 각 문자 개수 세기
  12. for (char c : str) {
  13. freq[c]++;
  14. }
  15.  
  16. char maxChar = 'z' + 1; // 사전순으로 가장 뒤에 있는 문자보다 큰 값으로 초기화
  17. int maxCount = 0;
  18.  
  19. // 최빈 문자 찾기
  20. for (auto &[c, count] : freq) {
  21. if (count > maxCount || (count == maxCount && c < maxChar)) {
  22. maxChar = c;
  23. maxCount = count;
  24. }
  25. }
  26.  
  27. cout << maxChar << endl;
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 5288KB
stdin
yonsai
stdout
a