1일1알

백준 14713번 앵무새 C++ 본문

알고리즘

백준 14713번 앵무새 C++

영춘권의달인 2023. 2. 27. 13:34

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

 

14713번: 앵무새

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

www.acmicpc.net

 

단어들이 순서대로 맞춰졌는지와 모든 단어가 사용되었는지 있는지 확인해야한다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int n;
vector<queue<string>> v;

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

    cin >> n;
    cin.ignore();
    v = vector<queue<string>>(n);
    string sentence;
    for (int i = 0; i < n; i++) {
        getline(cin, sentence);
        int front = 0;
        string tmp = "";
        for (int j = 0; j <= sentence.length(); j++) {
            if (sentence[j] != ' ' && j != sentence.length()) continue;
            tmp = sentence.substr(front, j - front);
            front = j + 1;
            v[i].push(tmp);
        }
    }
    getline(cin, sentence);
    int front = 0;
    string tmp = "";
    queue<string> q;
    for (int i = 0; i <= sentence.length(); i++) {
        if (sentence[i] != ' ' && i != sentence.length()) continue;
        tmp = sentence.substr(front, i - front);
        front = i + 1;
        q.push(tmp);
    }
    int cnt = 0;
    while (!q.empty()) {
        string curr = q.front();
        q.pop();
        bool ans = false;
        for (int i = 0; i < n; i++) {
            if (v[i].empty()) continue;
            if (v[i].front() != curr) continue;
            ans = true;
            v[i].pop();
            if (v[i].empty()) cnt++;
            break;
        }
        if (ans == false) {
            cout << "Impossible";
            return 0;
        }
    }
    if (cnt != n) cout << "Impossible";
    else cout << "Possible";
}

 

'알고리즘' 카테고리의 다른 글

백준 20208번 진우의 민트초코우유 C++  (0) 2023.03.01
백준 11332번 시간초과 C++  (1) 2023.02.28
백준 1972번 놀라운 문자열 C++  (0) 2023.02.25
백준 18513번 샘터 C++  (0) 2023.02.24
백준18429번 근손실 C++  (0) 2023.02.23