앵무새 14713

PublicMinsu·2023년 5월 25일
0

문제

접근 방법

문장을 이루는 모든 단어에 대해서 모든 앵무새의 단어를 확인해도 10,000*100이기에 시간 안에 가능하다.
앵무새가 말하는 단어는 2번 이상 등장하지 않기에 순서가 잘못되어서 틀리는 경우는 없다. (앞에 앵무새에게 순서를 빼앗겨서 뒤에 단어를 사용하지 못하는 경우가 없다는 뜻이다)

코드

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
vector<queue<string>> bird;
vector<string> write;
int N;
int main()
{
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N;
    cin.ignore();
    bird = vector<queue<string>>(N, queue<string>());
    for (int i = 0; i <= N; ++i)
    {
        string j, k;
        getline(cin, j);
        stringstream ss(j);
        while (getline(ss, k, ' '))
        {
            if (i != N)
                bird[i].push(k);
            else
                write.push_back(k);
        }
    }
    for (string w : write)
    {
        bool isCorr = false;
        for (int i = 0; i < N; ++i)
        {
            if (bird[i].empty())
                continue;
            if (bird[i].front() == w)
            {
                bird[i].pop();
                isCorr = true;
                break;
            }
        }
        if (!isCorr)
        {
            cout << "Impossible";
            return 0;
        }
    }
    for (int i = 0; i < N; ++i)
        if (!bird[i].empty())
        {
            cout << "Impossible";
            return 0;
        }
    cout << "Possible";
    return 0;
}

풀이

C++의 경우 공백이 존재하는 문장을 받아서 처리해 줘야 하는 귀찮은 부분이 존재한다.

받아 적은 문장을 완성했어도 앵무새의 단어가 남으면 틀린다. (2%에서 틀린다)
이점을 눈치 못 채고 문장 입력을 잘못 받은 줄 알았다. 문제에서 대놓고 요구하지 않았기에 놓쳐버린 요소이다.

profile
연락 : publicminsu@naver.com

0개의 댓글