본문 바로가기
Algorithm/문자열

[백준][1620번][문자열] 나는야 포켓몬 마스터 이다솜

by 우툴 2016. 4. 13.
728x90

나는야 포켓몬 마스터 이다솜

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

<코드>

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
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <iostream>
#include <string>
#include <map>
using namespace std;
string c[100001];
 
map<stringint> abc;
 
int main(void){
    int N, M;
 
    scanf("%d %d", &N, &M);
 
    for (int i = 0; i < N; i++){
        char name[30];
        scanf("%s", name);
        c[i] = name;
        abc.insert(map<stringint>::value_type(c[i], i));
    }
 
 
 
    for (int i = 0; i < M; i++)
    {
        int num=0;
        char cc;
        string s;
        char name[30];
        scanf("%s", name);
        s=name;
 
 
        cc = s[0];
        if (cc >= '0' && cc <= '9')
        {
            for (int i = 0; s[i] != 0; i++)
            {
                num *= 10;
                num += s[i] - '0';
            }
             printf("%s\n", c[num - 1].c_str());
        }
        else
        {
            int left = 0, right = N - 1;
            int result;
            result = abc[s];
 
            printf("%d\n", result + 1);
        }
 
    }
}
cs

<문제 푼 요령>

1. 이 문제는 string과 map을 쓸 수 있다면 간단히 풀 수 있다. 

2. string을 쓰는 이유는 문자열끼리 비교가 쉽기 때문이다. 오름 차순이든 내림차순이다.

3. 그리고 여기서 cin, cout을 쓰면 굉장히 느려지는데 이건 유의 해야 된다. cin, cout을 쓰고 시간 초과가 놨는데 문제가 어딘지 몰라서 한참 해맸다.

728x90

댓글