본문 바로가기
알고리즘/문자와 문자열

9933 민균이의 비밀번호

by tryotto 2020. 1. 6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
map<stringint> mp;
 
int main() {
    int n;
    scanf("%d"&n);
 
    while (n--) {
        string str;
        cin >> str;
 
        string rev = str;
        reverse(rev.begin(), rev.end());
 
        if (str == rev) {
            int len = rev.size();
            int idx = len / 2;
 
            printf("%d %c", len, str.at(idx));
            break;
        }
        else if ((mp.find(rev) == mp.end())||(mp.find(str)==mp.end())) {
            mp[str] = 1;
            mp[rev] = 1;
        }        
        else {
            int len = rev.size();
            int idx = len / 2;
 
            printf("%d %c", len, rev.at(idx));
 
            break;
        }
    }
}
cs



이번에도 마찬가지로, string 으로 인덱싱이 필요해서 map 헤더를 사용했다.

근데 map 을 사용할 경우, 헤더를 불러오는데에 시간이 꽤 걸리는 것 같다. 염두하자.


또한, string 을 뒤집는 경우도 써야할 때가 있는데, 그걸 위해서 algorithm 의 reverse 함수를 사용했다.
이것도 유용하니 알아두자.


level 과 같은, 자기 자신이 출력되는 경우도 염두하자

'알고리즘 > 문자와 문자열' 카테고리의 다른 글

3077 임진왜란  (0) 2020.01.06
4949 균형잡힌 세상  (0) 2020.01.06
3986 좋은 단어  (0) 2020.01.06
10551 STROJOPIS  (0) 2020.01.06
5218 알파벳 거리  (0) 2020.01.05