카테고리 없음

백준 1406 에디터 (C++)

수박 서리 2022. 8. 23. 17:51

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

 

1406번: 에디터

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

www.acmicpc.net

스택보다 list를 사용하는게 더 쉬울 것 같아 . 리스트로 문제를 풀었다.

cursor는 iter로 해결하였음

 

 

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
#include <iostream>
#include <list>
#include <string>
 
using namespace std;
 
int main()
{
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    string str; //문자열
    int size// 입력 갯수
    list<char> answer;
    
    cin >> str; //문자열 입력
    
    for(int i = 0 ; i < str.length();i++)
        answer.push_back(str[i]);
    
    auto cursor = answer.end(); // 커서 위치 맨 마지막
    
    cin >> size;
    
    
    while(size--)
    {
        char command; //입력
        cin >> command;
        
        if(command == 'L')
        {
            if(cursor != answer.begin())//맨 왼쪽이 아니면 한 칸 이동
                cursor--;
 
        }else if(command == 'D')
        {
            if(cursor != answer.end()) //맨 오른쪽이 아니면 한 칸 이동
                cursor++;
 
        }else if(command == 'B')
        {
            if(cursor != answer.begin())//맨 왼쪽이 아니면 커서 왼쪽 위치 삭제
                answer.erase(--cursor); //커서 앞쪽 삭제
            
        }else if(command == 'P')
        {
            char ch;
            cin>>ch;
            answer.insert(cursor , ch);
        }
 
    }
    
    for(auto iter = answer.begin(); iter != answer.end(); iter++)
    {
        cout <<*iter;
    }
 
    return 0;
}
cs