본문 바로가기
알고리즘/백준

백준 1620번(나는야 포켓몬 마스터 이다솜) c++

by Jo_Wick 2023. 4. 16.
반응형

문제

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

 

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

첫째 줄에는 도감에 수록되어 있는 포켓몬의 개수 N이랑 내가 맞춰야 하는 문제의 개수 M이 주어져. N과 M은 1보다 크거나 같고, 100,000보다 작거나 같은 자연수인데, 자연수가 뭔지는 알지? 모르면

www.acmicpc.net

문제 풀이

이 문제는 Map 혹은 Hash로 아주 쉽게 풀 수 있다.

 

C++ Map 사용법

https://go-jowick.tistory.com/15

 

C++ STL Map 사용법

Map이란? map은 각 노드가 key와 value로 이루어진 트리이다. 중복을 허용하지 않는게 특징이다. 그리고 map은 검색, 삽입, 삭제 등이 O(logn)인 레드블랙트리로 구성되어 있다. map map1; map은 기본적으로

go-jowick.tistory.com

하나의 맵과 하나의 string 배열을 만들어 입력을 받아 각각 저장을 시켜주어 출력을 시키면 된다.

 

전체 코드

#include <bits/stdc++.h>
#include <string>

using namespace std;

int main(){

        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int N, M;

        cin >> N >> M;

        map<string, int> input;
        string name[100001];

        string temp;

        for(register int i = 1; i <= N; i++){

                cin >> temp;

                input.insert(pair<string, int>(temp, i));
                name[i] = temp;

        }

        for(register int i = 0; i < M; i++){

                cin >> temp;

                if(temp[0] >= '1' && temp[0] <= '9'){

                        int num = stoi(temp);

                        cout << name[num] << '\n';

                }

                else{
                        cout << input.find(temp)->second << '\n';
                }

        }

        return 0;

}

 

 

Hash 개념 정리

https://go-jowick.tistory.com/14

 

Hash c++ 개념 정리

Hash Hash는 기본적으로 임의의 길이의 문자열을 받아 고유한 key값을 생성한다. 그리고 그 key값에 value를 저장한다. 이렇게 저장을 할 때 같은 key값이 나올 수도 있다. 그래서 그러한 경우에는 Chanin

go-jowick.tistory.com

Hash도 마찬가지로 하나의 Hash 테이블과 하나의 string 배열을 만들어 각각 저장 시켜주어 출력시키면 된다.

 

전체 코드

#include <bits/stdc++.h>

#define MAX_TABLE 100001

using namespace std;

struct Node{

        string str;
        int num;
        Node *next;

};

Node node_head[MAX_TABLE];
Node node[MAX_TABLE];
int node_cnt;

string num_list[MAX_TABLE];

int hashing(string input){

        int div = 401;

        int i = 0;
        int len = input.length();

        while(i != len){
                div = ((div << 5) + (int)input[i]) % MAX_TABLE;
                i++;
        }

        return div % MAX_TABLE;

}

void insert_str(string input, int num){

        int key = hashing(input);

        Node *temp = &node_head[key];
        Node *target = &node[node_cnt++];

        target->str = input;
        target->num = num;

        if(temp->next == 0){
                temp->next = target;
        }
        else{
                while(temp->next != 0){
                        temp = temp->next;
                }
                temp->next = target;
        }

        return;

}

int search_str(string input){

        int key = hashing(input);

        Node *temp = &node_head[key];

        while(temp->next != 0){
                if(temp->next->str == input){
                        break;
                }
                temp = temp->next;
        }

        return temp->next->num;

}

int main(){

        ios_base::sync_with_stdio(false);
        cin.tie(0);

        int N, M;

        cin >> N >> M;

        string temp;

        for(register int i = 1; i <= N; i++){
                cin >> temp;
                num_list[i] = temp;
                insert_str(temp, i);
        }

        for(register int i = 0; i < M; i++){
                cin >> temp;
                if(temp[0] >= '1' && temp[0] <= '9'){
                        int num_temp = stoi(temp);
                    
                        cout << num_list[num_temp] << '\n';
                }
                else{
                        cout << search_str(temp) << '\n';
                }
        }

        return 0;

}
반응형

'알고리즘 > 백준' 카테고리의 다른 글

백준 9012번(괄호)  (0) 2023.04.17
백준 9093번 (단어 뒤집기)  (0) 2023.04.17
백준 1269번 (대칭 차집합) c++  (0) 2023.04.14
백준 1065번(한수) c++  (0) 2023.04.09
백준 1018번 (체스판 다시 칠하기) c++  (0) 2023.04.09

댓글