https://www.acmicpc.net/problem/9935
개인적으로 재미있게 푼 문제입니다.
문자열을 문자단위로 쪼개서 Stack에 넣고 쪼개져 들어오는 문자를 하나씩 검사해
"현재 바라보고있는 폭발문자열의 문자" 와 비교하여 같으면 "폭발문자열을 바라보는 인덱스" 를 하나씩 증가시켜주고
바라보고 있는 문자와 다를경우에는 폭발 문자열을 바라보는 인덱스를 다시 0번으로 초기화 시켜주었습니다.
폭발 이후에 앞에서 이미 검사한 문자 중 폭발 가능성이 있는 문자가 있을수 있기 때문에 폭발 문자열의 개수만큼 다시
tmp stack에 넣고 진행하였습니다.
#include<iostream>
#include<stack>
#include<algorithm>
#include<string>
#pragma warning (disable:4996)
using namespace std;
// 9935 문자열 폭발
int lenArr[36];
int main(){
string s,b;
stack<char>st;
stack<char>tmp;
cin >> s >> b;
while (!s.empty()) {
tmp.push(s.back());
s.pop_back();
}
int len = 0;
while (!tmp.empty()) {
//cout << tmp.top() << " " << b[len] << endl;
if (tmp.top() == b[len]) {
len++;
}
else {
len = 0;
if (tmp.top() == b[len])
continue;
}
st.push(tmp.top()); tmp.pop();
if (len == b.length()) {
while (len--) {
st.pop();
}
for (int i = 0; i < b.length() && !st.empty();i++) {
tmp.push(st.top()); st.pop();
}
len = 0;
}
}
if (st.empty()) {
cout << "FRULA";
return 0;
}
while (!st.empty()) {
s.push_back(st.top()); st.pop();
}
reverse(s.begin(), s.end());
cout << s;
}
'Algorithm > Baekjoon online judge' 카테고리의 다른 글
[BOJ] 17071 숨박꼭질5 (0) | 2020.04.04 |
---|---|
[BOJ] 2580 스도쿠 (0) | 2020.04.02 |