반응형
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;
}

반응형
'Algorithm > problem' 카테고리의 다른 글
백준 2636번 : 치즈 BFS C++ (0) | 2022.02.13 |
---|---|
백준 1753번 : 최단 경로 - 다익스트라 C++ 코드 (0) | 2022.02.13 |
백준 2042번 : 구간 합 구하기 - Segment Tree C++ (0) | 2022.02.11 |
백준 9935번 : 문자열 폭발 - C++ 스택 직접 구현 (0) | 2022.02.10 |
백준 2812번 : 크게 만들기 (0) | 2022.02.10 |