GGym's Practice Notes

백준 9935 : 문자열 폭발 본문

Algorithm

백준 9935 : 문자열 폭발

GGym_ 2021. 4. 28. 21:28

스택 문제

스택에 폭발 문자열의 앞부분을 쌓아두고 터지면 스택에 있는 것을 pop하면서 없애면 된다.

스택에 쌓이는 도중에 다른 문자열이 들어오면 스택 앞에 있는 문자열은 터지지 않으므로 스택을 비워놓는 것이 중요하다.

 

#include <cstdio>   //9935_문자열폭발 [스택]
#include <stack>
#include <iostream>
using namespace std;

int main(){
    stack<int> S;
    char str[1000001];
    char boom[1000001];
    string result = "";

    scanf("%s %s", str, boom);
    int l;
    for(l=0; boom[l]!='\0'; l++);
    for(int i=0; str[i] != '\0';i++){
        if (boom[0] == str[i])
            S.push(1);

        else if (!S.empty() && boom[S.top()] == str[i])
            S.top()++;

        else while(!S.empty()) S.pop();
        result += str[i];

        if(!S.empty() && S.top() == l) {
            S.pop();
            result.erase(result.size()-l, l);
        }
    } 

    if(result.size() == 0)printf("FRULA");
    else printf("%s", result.c_str());

    return 0;
}

'Algorithm' 카테고리의 다른 글

백준 2042 : 구간 합 구하기  (0) 2021.04.28
백준 1062 : 가르침  (0) 2021.04.28
백준 1167 : 트리의 지름  (0) 2021.04.20
프로그래머스 Level2_ 수식 최대화 (C++)  (0) 2020.10.31
프로그래머스 Level2_ 프린터 (C++)  (0) 2020.10.31