<< 문제 >>
https://www.acmicpc.net/problem/9935
<< 풀이 >>
Stack을 사용합니다.
이전에는 Stack에 문자열을 집어 넣을때 폭발 문자열을 확인하려고 했었는데, 이것저것 생각할 것들이 너무 많아졌습니다.
그래서 서칭을 하던 중 Stack에 집어넣은 뒤에 확인하는 방법이 있더군요.
이 방법을 사용했습니다.
<< 코드 >>
#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;
int main() {
string str;
cin >> str;
string bomb;
cin >> bomb;
char lastCh = bomb[bomb.size()-1];
stack<char> st;
for(auto& a : str)
{
st.push(a);
if(st.top() == lastCh && st.size() >= bomb.size()) // 마지막 글자와 같다면
{
string temp;
for(int i = 0; i < bomb.size(); i++) // 다시 빼서 문자열로 만들기
{
temp += st.top();
st.pop();
}
reverse(temp.begin(), temp.end()); // 문자열 뒤집기
if(temp.compare(bomb) == -1){ // 폭탄 문자열과 비교
for(int j = 0; j < temp.size(); j++){
st.push(temp[j]);
}
}
}
}
string result;
while(!st.empty())
{
result += st.top();
st.pop();
}
reverse(result.begin(), result.end());
if(result.size() == 0)
{
cout << "FRULA";
}
else{
cout << result;
}
}
<< 깃헙 >>
StudyAlgorithm_Part2/백준/Gold/9935. 문자열 폭발 at main · J2on/StudyAlgorithm_Part2
This is a auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - J2on/StudyAlgorithm_Part2
github.com
< 백준 BaekJoon : 13305번 주유소 > C++ (0) | 2024.07.02 |
---|---|
< 백준 BaekJoon : 1026번 보물 > C++ (0) | 2024.06.20 |
< 백준 BaekJoon : 17520번 Balanced String > C++ (0) | 2024.06.20 |
< 백준 BaekJoon : 1931번 회의실 배정 > C++ (1) | 2024.06.18 |
< 백준 BaekJoon : 20058번 마법사 상어와 파이어스톰 > C++ (0) | 2024.04.09 |
댓글 영역