알고리즘
백준 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";
}