본문 바로가기

Algorithm

C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 9935번:문자열 폭발 답

반응형

스텍 문제 입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <stack>
using namespace std;
char word[1000001],bomb[36];
stack <pair<int,int>> bombCheck;//first는 word에서 폭발 문자열을 발견했을 때 그 위치 저장,second는 bomb의 인덱스를 저장
bool e[1000001];
int cnt;
int main(){
    cin >> word >> bomb;
    int sWord = strlen(word);
    int sBomb = strlen(bomb);//문자와 폭발문자열의 크기 저장
    if (sBomb == 1)//폭발 문자열이 1개라면 stack에 넣을 필요가 없으므로 그냥 for문
    {
        for (int i = 0; i < sWord; i++)
        {
            if (word[i] == bomb[0])
                e[i] = 1;
        }
    }
    else //폭발 문자열의 길이가 2이상이라면
    {
        for (int i = 0; i < sWord; i++)
        {
            if (word[i] == bomb[0])//폭발 문자열의 처음과 같다면 그 처음의 인덱스를 저장해준다
                bombCheck.push(make_pair(i, 0));
            else
            {
                if (!bombCheck.empty())//스텍이 비어있지 않다면 
                {
                    auto tmp = bombCheck.top();//스텍의 가장 위의 인덱스를 저장
                    if (word[i] == bomb[tmp.second + 1])//폭발 문자열의 최근의 다음 번째와 같다면 
                    {
                        bombCheck.push(make_pair(i, tmp.second + 1));//폭발 문자열의 인덱스 1추가한 것을 넣어줌
                        if (tmp.second + == sBomb - 1)//스텍의 가장 위 인덱스가 폭발문자열의 길이와 같다면
                        {
                            for (int j = 0; j < sBomb; j++)
                            {
                                auto tmp = bombCheck.top();
                                e[tmp.first] = 1;
                                bombCheck.pop();
                            }
                        }
                    }
                    else//폭발 문자열의 일부가 아닐경우
                    {
                        while (!bombCheck.empty())
                            bombCheck.pop();
                    }
                }
            }
        }
    }
    bool sch = 0;
    for (int i = 0; i < sWord; i++)
    {
        if (e[i]) { continue; }//지운 부분은 출력 x
        cout << word[i];
        sch = 1;
    }
    if (!sch)//다 지워졌을 경우
        cout << "FRULA" << '\n';
}
cs