문제풀이/백준

1157 - 단어 공부

동바리 2021. 8. 24. 14:50
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    string testStr;
    cin >> testStr;
    int alphabetCnt[26] = {0};
    int max = 0,
        maxIdx = 0;
    
    for(int i = 0 ; i < testStr.size(); i++) {
        if(testStr[i] > 90) {
            testStr[i] = toupper(testStr[i]);
        }

        alphabetCnt[testStr[i] - 65]++;
        if(max < alphabetCnt[testStr[i] - 65]) {
            max = alphabetCnt[testStr[i] - 65];
            maxIdx = testStr[i] - 65;
        }
    }

    for(int i = 0 ; i < 26; i++) {
        if(alphabetCnt[i] == alphabetCnt[maxIdx] && maxIdx != i) {
            cout << '?' << '\n';
            return 0;
        }
    }

    cout << (char)(maxIdx + 65) << '\n';
    return 0;
}

알파벳 관련된 문제는 아스키 코드를 사용하자. 다시금 기초의 중요함을 느낀다.

풀이는 하단의 사이트를 참고했습니다. 감사합니다.

https://mtoc.tistory.com/26