알고리즘
백준 9935번 문자열 폭발 C++
영춘권의달인
2022. 3. 28. 12:10
1. 문자열을 처음부터 순회하면서 스택에 하나씩 넣는다.
2. 폭발 문자열의 마지막 문자와 지금 순회중인 문자가 같다면 스택에서 폭발 문자열의 크기만큼 뺀다.
3. 뺀 문자열과 폭발 문자열을 비교하여 같다면 그대로 지나가고 같지 않다면 뺀 문자열을 다시 스택에 넣는다.
4. 순회가 끝났으면 스택에서 문자를 전부 빼서 역순으로 출력한다.
#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 <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str, target;
cin >> str >> target;
stack<char> st;
char targetChar = target[target.length() - 1];
for (int i = 0; i < str.length(); i++) {
st.push(str[i]);
if (str[i] == targetChar) {
if (st.size() >= target.length()) {
string tmp = "";
for (int j = 0; j < target.length(); j++) {
tmp += st.top();
st.pop();
}
reverse(tmp.begin(), tmp.end());
if (tmp != target) {
for (int j = 0; j < tmp.length(); j++) {
st.push(tmp[j]);
}
}
}
}
}
string ans = "";
while (!st.empty()) {
ans += st.top();
st.pop();
}
reverse(ans.begin(), ans.end());
if (ans == "") {
cout << "FRULA";
}
else {
cout << ans;
}
};