[C++][백준 31562] 전주 듣고 노래 맞히기

PublicMinsu·2024년 3월 10일

문제

접근 방법

3개의 음만이 음악 식별에 도움이 된다.
3개의 음을 가지고 이미 나온 노래가 있는지 확인해 주면 된다.

코드

#include <iostream>
#include <unordered_map>
using namespace std;
unordered_map<string, string> um;
int N, M, T;
char a[7];
string song;
string getThree()
{
    string str;
    for (int i = 0; i < 3; ++i)
    {
        str += a[i];
    }
    return str;
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N >> M;

    while (N--)
    {
        cin >> T >> song;
        for (char &c : a)
        {
            cin >> c;
        }
        string str = getThree();

        if (um.find(str) == um.end())
        {
            um[str] = song;
        }
        else
        {
            um[str] = "?";
        }
    }

    while (M--)
    {
        for (int i = 0; i < 3; ++i)
        {
            cin >> a[i];
        }

        string str = getThree();

        bool isFind = um.find(str) != um.end();
        cout << (isFind ? um[str] : "!") << "\n";
    }
    return 0;
}

풀이

3개의 음과 동일한 노래가 있는지 확인하고 없다면 음악을 저장하고 있다면 ?로 바꾸어 준다.

만약 맞히기를 시도할 때 아무 값도 없다면 동일한 노래가 없었다는 뜻이므로 !를 출력한다.

profile
연락 : publicminsu@naver.com

0개의 댓글