[C++] 1157: 단어 공부

쩡우·2023년 1월 23일
0

BOJ algorithm

목록 보기
50/65

문제

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

입력

첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.

출력

첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.

코드

#include <iostream>

using namespace std;

char input[1000001];
int count[26];

int main(void)
{
	cin >> input;

	int i = -1;
	while (input[++i])
	{
		if (input[i] >= 'a')
			count[input[i] - 'a']++;
		else
			count[input[i] - 'A']++;
	}

	int result = 0;
	i = 0;
	while (++i < 26)
		if (count[result] < count[i])
			result = i;

	i = -1;
	while (++i < 26)
	{
		if (count[result] == count[i] && result != i)
		{
			cout << '?' << '\n';
			return (0);
		}
	}

	char a = result + 'A';
	cout << a << '\n';

	return (0);
}

이지

profile
Jeongwoo's develop story

0개의 댓글