백준 c++/(1-1)백준 c++ 알고리즘 기초

백준 1406 c++ 에디터

현구구 2022. 7. 4. 14:24

https://www.acmicpc.net/problem/1406

 

1406번: 에디터

첫째 줄에는 초기에 편집기에 입력되어 있는 문자열이 주어진다. 이 문자열은 길이가 N이고, 영어 소문자로만 이루어져 있으며, 길이는 100,000을 넘지 않는다. 둘째 줄에는 입력할 명령어의 개수

www.acmicpc.net


처음 해봤던 코드이다.

1개의 stack, 1개의 int형 cursor로 진행을 해봤다.

결과는 메모리 초과... 

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
 
 
int main()
{
    string s;
    int count;
    cin >> s;
    char getcase;
    stack<char> charstack;
    int cursor= s.size();
    cin >> count;
    while (count--)
    {
        cin >> getcase;
        char ip;
        if (getcase == 'P')
        {
            cin >> ip;
            if (cursor==s.size())
            {
                s = s + ip;
            }
            else
            {
                for (int i = s.size() - 1; i >= cursor; i--)
                {
                    charstack.push(s[i]);
                    s[i] = NULL;
                }
                s[cursor] = ip;
            }
            while (charstack.empty() == 0)
            {
                s += charstack.top();
                charstack.pop();
            }
            cursor++;
        }
        if (getcase == 'L')
        {
            if (cursor == 0)
            {
                continue;
            }
            else
            {
                cursor--;
            }
        }
        if (getcase == 'D')
        {
            if (cursor == s.size())
            {
                continue;
            }
            else
            {
                cursor++;
            }
        }
        if (getcase == 'B')
        {
            if (cursor == 0)
            {
                continue;
            }
            else
            {
                s[cursor - 1= NULL;
                cursor--;
            }
        }
    }
    cout << s;
}
cs

 


아래는 다른 방법을 찾아보았다.

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
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
 
 
int main() {
    string s; //처음에 입력받을 문자열
    cin >> s;
    int count;
    cin >> count;//에디터 진행할 횟수
    stack<char> stack1; //주 스택
    stack<char> stack2; //보조 스택
    char getcase;//L,D,B,P 4개의 케이스
    for (int i = 0; i < s.size(); i++)//처음 입력받은 문자열을 stack1에 넣음
    {
        stack1.push(s[i]);
    }
    while (count--)//count횟수 만큼 반복
    {
        cin >> getcase;
        if (getcase == 'P')//P일 경우 char형 변수 입력 받아서 stack1에 추가
        {
            char addr;
            cin >> addr;
            stack1.push(addr);
        }
        if (getcase == 'L')//L일 경우
        {
            if (stack1.empty() == 1)//stack1이 비어있다면 pass
            {
                continue;
            }
            else//stack1이 안 비어있다면 stack1의 top을 stack2로 이동
            {
                stack2.push(stack1.top());
                stack1.pop();
            }
        }
        if (getcase == 'D')//D일 경우
        {
            if (stack2.empty() == 1)//stack2가 비어있다면 pass
            {
                continue;
            }
            else//stack2가 안 비어있다면 stack2의 top을 stack1으로 이동
            {
                stack1.push(stack2.top());
                stack2.pop();
            }
        }
        if (getcase == 'B')//B일 경우
        {
            if (stack1.empty() == 1)//stack1이 비어있다면 pass
            {
                continue;
            }
            else//아니라면 stack1 pop
            {
                stack1.pop();
            }
        }
    }
    while (stack1.empty() != 1)//stack1이 빌 때 까지 stack2로 이동
    {
        stack2.push(stack1.top());
        stack1.pop();
    }
    while (stack2.empty() != 1)//stack2 top부터 전부 
    {
        cout << stack2.top();
        stack2.pop();
    } 
}
cs

 

stack1이 주 스택 stack2는 보조 스택이다

이때 커서는 stack1의 top을 가리킨다(고정)

P : 커서인 stack1의 top 에 새로운 char push

L : 커서를 한칸 왼쪽으로 옮기기 위해 stack1의 top을 stack2로 이동

D : 커서를 한칸 오른쪽으로 옮기기 위해 stack2에서 다시 stack1의 top으로 이동

B : 커서에 있는 것을 삭제 하기 위해 stack1의 top을 pop

 

'백준 c++ > (1-1)백준 c++ 알고리즘 기초' 카테고리의 다른 글

백준 1158 c++ 요세푸스 문제  (0) 2022.07.04
백준 10845 c++ 큐  (0) 2022.07.04
백준 c++ 1874 스택 수열  (0) 2022.07.04
백준 9012 c++ 괄호  (0) 2022.07.04
백준 9093 c++ 단어 뒤집기  (0) 2022.07.04