알고리즘/백준

백준 14713 앵무새

등반 2021. 12. 24. 23:47

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

 

14713번: 앵무새

자가용 비행기를 타고 세계 일주를 하던 pps789와 cseteram은 어느 날 엔진 고장으로 인해 이름 모를 섬에 불시착하게 된다. 그들은 이 섬을 탐험하는 도중 아주 신기한 사실을 알게 되었는데, 바로

www.acmicpc.net

큐를 사용할 수 있는지에 관한 문제다.

앵무새들을 모두 큐로 만들고, 목표 문장에서 현재 나와야하는 단어를 말 할 수 있는 앵무새(큐)를 하나하나 찾아주면 된다.

#include <iostream>
#define fio cin.tie(0)->sync_with_stdio(0)
using namespace std;

#include <queue>
#include <sstream>
queue<string> bird[101];
int main(){
    fio;
    int N; cin >> N;    cin.ignore();
    for(int i=0; i<N; i++){
        string tmp;
        getline(cin, tmp);
        stringstream ss(tmp);
        
        string tmps;
        while(ss >> tmps){
            bird[i].push(tmps);
        }
    }

    //
    queue<string> Q;
    string tmp;
    getline(cin, tmp);
    stringstream ss(tmp);
    string tmps;
    while(ss >> tmps){
        Q.push(tmps);
    } 
    
    bool flg = true;
    while(!Q.empty()){
        string now = Q.front();
        bool tmp_flg = false;
        for(int i=0; i <N; i++){
            if(bird[i].front() == now){
                bird[i].pop();
                Q.pop();
                tmp_flg=true;
                break;
            }
        }
        if(!tmp_flg){
            flg = false;
            break;
        }
    }

    for(int i=0; i <N; i++){
        if(!bird[i].empty()) flg = false;
    }
    
    if(flg) cout << "Possible";
    else cout << "Impossible";
    return 0;
}

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

백준 2504 괄호의 값  (0) 2021.12.25
백준 1662 압축  (0) 2021.12.25
백준 12789 도키도키 간식드리미  (0) 2021.12.24
백준 18100 Думский регламент  (0) 2021.12.24
백준 15815 천재 수학자 성필  (0) 2021.12.24