Algorithm/problem
백준 3111번 : 검열 - deque 활용 코드 C++
DingCoDing
2022. 2. 12. 21:21
반응형
https://www.acmicpc.net/problem/3111
3111번: 검열
첫째 줄에 단어 A가, 둘째 줄에 텍스트 T가 주어진다. A와 T는 알파벳 소문자로만 이루어져 있고, A는 최대 25자, T는 최대 300,000자이다.
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
string a, t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> a >> t;
int l = 0, r = t.length() - 1, turn = 0;
deque<char> left, right;
while (l <= r) {
if (turn % 2 == 0) {
left.push_back(t[l++]);
if (left.size() >= a.length()) {
bool is_same = true;
for (int i = 0; i < a.length(); i++) {
if (a[a.length() - 1 - i] != left[left.size() - 1 - i]) {
is_same = false;
break;
}
}
if (is_same) {
for (int i = 0; i < a.length(); i++)
left.pop_back();
turn++;
}
}
}
else {
right.push_front(t[r--]);
if (right.size() >= a.length()) {
bool is_same = true;
for (int i = 0; i < a.length(); i++) {
if (a[i] != right[i]) {
is_same = false;
break;
}
}
if (is_same) {
for (int i = 0; i < a.length(); i++)
right.pop_front();
turn++;
}
}
}
}
while (!right.empty()) {
left.push_back(right.front());
right.pop_front();
if (left.size() >= a.length() && left.back() == a[a.length() - 1]) {
bool is_same = true;
for (int i = 0; i < a.length(); i++) {
if (a[a.length() - 1 - i] != left[left.size() - 1 - i]) {
is_same = false;
break;
}
}
if (is_same) {
for (int i = 0; i < a.length(); i++)
left.pop_back();
}
}
}
while (!left.empty()) {
cout << left.front();
left.pop_front();
}
cout << '\n';
return 0;
}
반응형