[伯俊]1620ポケモンマスター李多順です


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

質問する


...
(省略)
...
呉博士:そんなに順調ですね.本当のポケモンマスターになるために、図鑑を完成させましょう.まず、あなたが持っているポケモン図鑑で、ポケモンの名前を見るとポケモンの番号が出たり、ポケモンの番号を見るとポケモンの名前が出たりする練習をします.もし私が試験に合格したら、新しく作った図鑑をあげます.

に答える


図鑑番号と名前がkeyとvalueのディックシャナリー.
名前と番号keyとvalueのディックシーケンスを作成して解析します.

Python

n, m= map(int, input().split())
dict1 = {}; dict2 = {}; answer=[]
for i in range(n):
    temp = input()
    dict1[str(i+1)] = temp
    dict2[temp] = str(i+1) 
for _ in range(m):
    temp = input()
    answer.append(dict1[temp] if temp.isdigit() else dict2[temp])
print("\n".join(answer))

C++

#include <bits/stdc++.h>
using namespace std; 
int n, m;
string temp;
map <string, int> map_1;
map <int, string> map_2;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cin>>n>>m;
    for (int i = 0; i < n; i++)
    {
        cin>>temp;
        map_1[temp] = i+1; 
        map_2[i+1] = temp;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> temp;
        if(atoi(temp.c_str()) == 0){ //문자열이면
            cout << map_1[temp] << "\n";
        }else{
            cout << map_2[atoi(temp.c_str())] << "\n";
        }
    }
    return 0;
}
}
ずっとタイムアウトを表示しているので、プレッシャーがかかります.
「n」ではなく「end」であれば、もっと時間がかかることがわかります.