앵무새 - 14713

2025. 2. 12. 15:17PS/백준

문제

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

느낀점

사실 구현 자체는 쉽다고 생각했다. 순서대로 왼쪽에서 하나씩 빼면서 비교하는 것이었는데, 자꾸 틀리는 것이었다. 문제를 잘 살펴보니 단어들의 갯수가 맞아야한다는 것을 깨달았고, 그 조건을 추가하니 통과했다.. 문제를 잘 읽자는 교훈을 얻음

풀이

#include <iostream>
#include <string>
#include <queue>
#include <map>

using namespace std;

bool judge(int N, map<int, queue<string>> &speech, string word)
{
    for (int i = 0; i < N; i++)
    {
        if (speech[i].empty())
        {
            continue;
        }

        string wordComparison = speech[i].front();

        if (wordComparison == word)
        {
            speech[i].pop();

            return true;
        }
    }
    return false;
}

queue<string> makeSplitString(queue<string> &sQueue, string s)
{
    string sStack = "";
    for (size_t j = 0; j < s.size(); j++)
    {
        if (s[j] == ' ')
        {
            sQueue.push(sStack);
            sStack = "";
        }
        else
        {
            sStack += s[j];
        }

        if (j == s.size() - 1)
        {
            sQueue.push(sStack);
        }
    }

    return sQueue;
}
int main()
{
    int N, speechLength = 0;
    cin >> N;
    cin.ignore();
    string temp;
    map<int, queue<string>> speech;

    for (int i = 0; i < N; i++)
    {
        queue<string> sQueue;
        getline(cin, temp);
        makeSplitString(sQueue, temp);
        speech[i] = sQueue;
        speechLength += sQueue.size();
    }
    string reference;
    queue<string> aQueue;
    getline(cin, reference);
    makeSplitString(aQueue, reference);
    int writeLength = aQueue.size();
    if (speechLength != writeLength)
    {
        cout << "Impossible";
        return 0;
    }

    while (!aQueue.empty())
    {
        string q = aQueue.front();
        aQueue.pop();
        if (!judge(N, speech, q))
        {
            cout << "Impossible";
            return 0;
        }
    }

    cout << "Possible";
    return 0;
}

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

비슷한 단어 - 2607  (0) 2025.02.12
오리 - 12933  (0) 2025.02.12
222-풀링 - 17829  (0) 2025.02.12
마인크래프트 - 18111  (0) 2025.02.12
도키도키 간식드리미 - 12789  (0) 2025.01.12