1 분 소요

Question

문제에 있는 내용은 푸는데 전혀 연관이 없으니 바로 입력부분 부터 보겠다.

1620Q

Solution

시간을 고려하여 map을 이용하여야 한다.

  • map의 key를 string을 받는다.
  • string으로된 배열에 따로 또 저장해준다.
  • 입력이 string 이면 map에서 해당 key의 value를 출력한다.
  • 입력이 int 이면 string배열에서 index를 통하여 string을 출력한다.

Cord

#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, int> poketStr;
string poketNum[100001];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, m;
    string input;
    cin >> n >> m;

    for (int idx = 0; idx < n; idx++)
    {
        cin >> input;

        poketStr.insert({ input, idx + 1 });
        poketNum[idx] = input;
    }

    for (int idx = 0; idx < m; idx++)
    {        
        cin >> input;

        if ((input[0] >= 'A' && input[0] <= 'Z') || (input[0] >= 'a' && input[0] <= 'z'))
        {
            cout << poketStr[input] << '\n';
        }
        else
        {
            cout << poketNum[stoi(input) - 1] << '\n';
        }
    }

    return 0;
}


Result

1620

앞으로 실패한 부분도 업로드 할 것이다.
시간 초과가 된 이유는 pair로 vector에 저장하고 for문으로 원하는 값을 찾아서 출력을했다.
그래서 시간초과가 발생한 것이다.

What`s new

mapset과 마찬가지로 tree 구조로 되어있다. key와 value를 가진다.
여기서는 key를 string 으로 받았다.

map<string, int> m;

set과 마찬가지로 insert()를 통해 요소를 추가할 수 있다.
요소를 추가할땐 {,} 배열형태나 make_pair(,) 를 이용한다.

m.insert({key, value});
m. insert(make_pair(key, value));