ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준 2204] 도비의 난독증 테스트
    백준/문자열 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;
    }

    '백준 > 문자열' 카테고리의 다른 글

    [백준 1316] 그룹 단어 체커  (0) 2024.02.23
    [백준 5052] 전화번호 목록  (2) 2024.01.10
    [백준 14426] 접두사 찾기  (0) 2024.01.10
    [백준 1283] 단축키 지정  (1) 2024.01.10
    [백준 3048] 개미  (0) 2023.03.23