상세 컨텐츠

본문 제목

< 백준 BaekJoon : 9935번 문자열 폭발> C++

C++/Baekjoon

by J2on 2024. 6. 20. 06:33

본문

<< 문제 >>

 

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;
	}
	
	
}

 

 

<< 깃헙 >>

https://github.com/J2on/StudyAlgorithm_Part2/tree/main/%EB%B0%B1%EC%A4%80/Gold/9935.%E2%80%85%EB%AC%B8%EC%9E%90%EC%97%B4%E2%80%85%ED%8F%AD%EB%B0%9C

 

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

 

관련글 더보기

댓글 영역