백준 1157, 단어 공부 C++ 성공 코드

카테고리 없음

2019. 2. 5. 14:47

01. 문제 풀이 과정


01) A ~ Z의 key해당 문자의 출몰횟수를 value로 가지는 pair관리용 vector 생성


02) 문자를 하나씩 입력받으며 해당 pair의 value 1 증가


03) 문자의 출몰횟수(value) 에 따라 정렬


04) 경우에 따라 출력


02. 코드

#include <bits/stdc++.h>
using namespace std;

int main(void)
{
	vector<pair<char, int>> wordVector;
	vector<pair<char, int>>::iterator it;
	char c;

	for(c='A'; c<='Z'; ++c) {
		wordVector.push_back(pair<char, int>(c, 0));
	}

	while(cin >> c) {
		c = toupper(c);
		for(it=wordVector.begin(); it!=wordVector.end() &&
			!(it->first==c); ++it)
			;
		if(it != wordVector.end())
			it->second += 1;
	}

	sort(wordVector.begin(), wordVector.end(),
		[](pair<char, int>& A, pair<char, int>& B) {
		return A.second > B.second;
	});

	it = wordVector.begin();
	if(it->second == (++it)->second)
		cout << '?';
	else
		cout << wordVector.begin()->first;

	return 0;
}


03. 참조


백준 1157, 단어 공부 - 문제 페이지 바로가기