백준/문자열

[백준 2204] 도비의 난독증 테스트

mintuchel 2023. 3. 26. 23:28

이 문제의 핵심은 두 가지이다

 

1. vector<string> 을 sort 하면 사전순으로 해준다

2. tolower을 이용하려면 string 변수 자체를 넣는게 아니라 해당 string을 구성하는 글자 하나씩 넣어야함

string original, temp;

cin >> original;
temp = original;

// 소문자로 변환
for (int k = 0; k < temp.size(); k++) {
	temp[k] = tolower(temp[k]);
}

 

unordered_map을 사용하여

소문자로 변환한 string을 key로 original string을 value로 넣어주어

답을 출력할때 O(1)로 출력할 수 있게 해주었다

 


#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	vector<string> v;
	unordered_map<string, string> um;

	int N;
	string str, temp;
	while (1) {
		v.clear();
		um.clear();

		cin >> N;

		if (N == 0) break;

		for (int i = 0; i < N; i++) {
			cin >> str;
			temp = str;

			// 소문자로 변환
			for (int k = 0; k < temp.size(); k++) {
				temp[k] = tolower(temp[k]);
			}

			um[temp] = str;
			v.push_back(temp);
		}

		sort(v.begin(), v.end());
		cout << um[v[0]] << "\n";
	}

	return 0;
}