Algorithm

[๋ฐฑ์ค€ 1620] ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

osean 2023. 4. 12. 19:37

๋ฌธ์ œ

 

1620๋ฒˆ: ๋‚˜๋Š”์•ผ ํฌ์ผ“๋ชฌ ๋งˆ์Šคํ„ฐ ์ด๋‹ค์†œ

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด

www.acmicpc.net

์ฒซ์งธ ์ค„์—๋Š” ๋„๊ฐ์— ์ˆ˜๋ก๋˜์–ด ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ๊ฐœ์ˆ˜ N์ด๋ž‘ ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผ ํ•˜๋Š” ๋ฌธ์ œ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ ธ. N๊ณผ M์€ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ธ๋ฐ, ์ž์—ฐ์ˆ˜๊ฐ€ ๋ญ”์ง€๋Š” ์•Œ์ง€? ๋ชจ๋ฅด๋ฉด ๋ฌผ์–ด๋ด๋„ ๊ดœ์ฐฎ์•„. ๋‚˜๋Š” ์–ธ์ œ๋“ ์ง€ ์งˆ๋ฌธ์— ๋‹ตํ•ด์ค„ ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ์–ด.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ํฌ์ผ“๋ชฌ์˜ ๋ฒˆํ˜ธ๊ฐ€ 1๋ฒˆ์ธ ํฌ์ผ“๋ชฌ๋ถ€ํ„ฐ N๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ํฌ์ผ“๋ชฌ๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™€. ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„์€ ๋ชจ๋‘ ์˜์–ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ์žˆ๊ณ , ๋˜, ์Œ... ์ฒซ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž์ด๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์–ด. ์•„์ฐธ! ์ผ๋ถ€ ํฌ์ผ“๋ชฌ์€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋งŒ ๋Œ€๋ฌธ์ž์ผ ์ˆ˜๋„ ์žˆ์–ด. ํฌ์ผ“๋ชฌ ์ด๋ฆ„์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 20, ์ตœ์†Œ ๊ธธ์ด๋Š” 2์•ผ. ๊ทธ ๋‹ค์Œ ์ค„๋ถ€ํ„ฐ ์ด M๊ฐœ์˜ ์ค„์— ๋‚ด๊ฐ€ ๋งž์ถฐ์•ผํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์™€. ๋ฌธ์ œ๊ฐ€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๋“ค์–ด์˜ค๋ฉด ํฌ์ผ“๋ชฌ ๋ฒˆํ˜ธ๋ฅผ ๋งํ•ด์•ผ ํ•˜๊ณ , ์ˆซ์ž๋กœ๋งŒ ๋“ค์–ด์˜ค๋ฉด, ํฌ์ผ“๋ชฌ ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•ด. ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ์ˆซ์ž๋Š” ๋ฐ˜๋“œ์‹œ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๊ณ , ์ž…๋ ฅ์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ฌธ์ž๋Š” ๋ฐ˜๋“œ์‹œ ๋„๊ฐ์— ์žˆ๋Š” ํฌ์ผ“๋ชฌ์˜ ์ด๋ฆ„๋งŒ ์ฃผ์–ด์ ธ. ๊ทธ๋Ÿผ ํ™”์ดํŒ…!!!

ํ’€์ด

 

๊ณต์œ  ์†Œ์Šค ๋ณด๊ธฐ

 

www.acmicpc.net

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

using namespace std;

vector<string> split(string input, string delimeter) {
    long long pos = 0;
    string token = "";
    vector<string> result;

    while ((pos = input.find(delimeter)) != string::npos) {
        token = input.substr(0, pos);
        result.push_back(token);
        input.erase(0, pos + delimeter.length());
    }
    result.push_back(input);
    return result;
}

vector<string> input(int n) {
    vector<string> result;
    for (int i = 0; i < n; i++) {
        string input = "";
        getline(cin, input);
        result.push_back(input);
    }
    return result;
}

map<string, int> createMap(vector<string> dictionary) {
    map<string, int> result;
    for (int i = 0; i < dictionary.size(); i++) {
        result[dictionary[i]] = i + 1;
    }
    return result;
}

int main() {
    // ํฌ์ผ“๋ชฌ ์ˆ˜, ๋ฌธ์ œ์ˆ˜ ์ž…๋ ฅ
    string nm = "";
    getline(cin, nm);
    vector<string> result = split(nm, " ");
    // Index To Name
    vector<string> indexToName = input(stoi(result[0]));
    // Name To Index
    map<string, int> nameToIndex = createMap(indexToName);
    // ๋ฌธ์ œ์˜ ํฌ์ผ“๋ชฌ
    vector<string> pokemons = input(stoi(result[1]));
    for (string pokemon: pokemons) {
        // nameToIndex ์˜ ๊ฐ’์ด 0 ์ด๋ฉด ์ด๋ฆ„ ์ถœ๋ ฅ ์•„๋‹ˆ๋ฉด ์ธ๋ฑ์Šค ์ถœ๋ ฅ
        if (nameToIndex[pokemon] == 0) cout << indexToName[stoi(pokemon) - 1] << "\n";
        else cout << nameToIndex[pokemon] << "\n";
    }
}