본문 바로가기
알고리즘 풀이

1157 단어 공부

by 남생이야 2024. 7. 29.

 

https://www.acmicpc.net/problem/1157

 

 

 

 

풀이 생각 

 => 대문자로 출력하니 미리 대문자로 변환 후, Map에 각각의 값들을 저장, 출력 시엔 Map을 검사하면서 samecount 값을 갱신하면서 samecount 값과 같고 다른 문자가 온다면 checkout 값을 증가 Map 전부를 확인 후에 checkout 값이 0이 아니라면 중복 문자가 있는 것이므로 ? 출력으로 해결해봄 

 

#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <cctype>
using namespace std;

int main()
{
	std::map<char, int> maps;
	string str; 
	cin >> str; 

	

	for (int i = 0; i < str.length(); i++)
	{
		char atk = toupper(str[i]);
		if (maps.find(atk) != maps.end())
			maps[atk] += 1;
		else
			maps[atk] = 1;
	}


	char bigger = '?';
	int samecount = 0; 
	int checkout = 0;
	for (map<char, int>::iterator itr = maps.begin(); itr != maps.end(); ++itr)
	{
		if (itr->second > samecount)
		{
			samecount = itr->second;
			bigger = itr->first;
			checkout = 0;
		}
		if (bigger != itr->first && samecount == itr->second)
		{
			checkout += 1;
		}
	}
	
	if (checkout == 0)
		cout << bigger;
	else
		cout << '?';

	return 0; 
}

'알고리즘 풀이' 카테고리의 다른 글

그룹 단어 체커  (0) 2024.07.29
크로아티아 알파벳  (0) 2024.07.29
2444번 별 찍기-7  (0) 2024.07.23
[백준] 킹, 퀸, 룩, 비숍, 나이트, 폰  (0) 2024.07.23
LCS - DP 이용  (0) 2024.07.13