본문 바로가기
공부/알고리즘

백준5397_키로거

by 미네밍 2016. 10. 31.

문제

창영이는 강산이의 비밀번호를 훔치기 위해서 강산이가 사용하는 컴퓨터에 키로거를 설치했다. 며칠을 기다린 끝에 창영이는 강산이가 비밀번호 창에 입력하는 글자를 얻어냈다. 하지만, 키로거는 사용자가 키보드를 누른 명령을 모두 기록한다. 따라서, 강산이가 비밀번호를 입력할 때, 화살표나 백스페이스를 입력하면 정확한 비밀번호를 알아낼 수가 없다.

강산이가 비밀번호 창에서 입력한 키가 주어졌을 때, 강산이의 비밀번호를 알아내는 프로그램을 작성하시오.

입력

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한줄로 이루어져 있고, 강산이가 입력한 순서대로 길이가 L인 문자열이 주어진다. (1 ≤ L의 길이 ≤ 1,000,000) 강산이가 백스페이스를 입력했다면, '-'가 주어진다. 이 때는 커서의 위치 바로 앞에 있는 글자를 지운다. 화살표의 입력은 '<'와 '>'로 주어진다. 이 때는 커서의 위치를 움직일 수 있다면, 왼쪽 또는 오른쪽으로 1만큼 움직인다. 나머지 문자는 비밀번호의 일부이다. 물론, 나중에 백스페이스를 통해서 지울 수는 있다. 만약 커서의 위치가 줄의 마지막이 아니라면, 그 문자를 입력하고, 커서는 오른쪽으로 한 칸 이동한다. 사실 설명을 읽기보다는 메모장을 켜서 이해하는 것을 추천한다.

출력

각 테스트 케이스에 대해서, 강산이의 비밀번호를 출력한다. 비밀번호의 길이는 항상 0보다 크다.

예제 입력 

2
<<BP<A>>Cd-
ThIsIsS3Cr3t

예제 출력 

BAPC
ThIsIsS3Cr3t




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
81
82
83
84
85
86
87
88
import java.util.Scanner;
 
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 명령 갯수
 
        for (int i = 0; i < n; i++) {
            String s = sc.next(); // 명령어
            List L = new List();
            for (int j = 0; j < s.length(); j++) {
                char cmd = s.charAt(j);
                int cnt;
                switch (cmd) {
                case '<': {
                    break;
                }
                case '>': {
                    break;
                }
                default: {
                    if (L.next == null) {
                        L.setcmd(cmd);
                        L.setnext(new List());
                    } else {
                        List curr = L.next;
                        while (curr.next != null) {
                            curr = curr.next;
                        }
                        curr.setcmd(cmd);
                        curr.setnext(new List());
                    }
                    break;
                }
                }
            }
 
            String c[] = new String[10000000];
            int end = 0;
            if (L.next == null) {
                c[end] = L.cmd + "";
                end++;
            } else {
                List curr = L;
                while (curr.next != null) {
                    if (curr.cmd == '-') {
                        if(end!=0){
                        c[end - 1= null;
                        end--;
                        }else{
                             
                        }
                    } else {
                        c[end] = curr.cmd+"";
                        end++;
                    }
                    curr = curr.next;
                }
                 
            }
             
            for(int j = 0 ; j < end; j++){
                System.out.print(c[j]);
            }
            System.out.println();
             
        }
    }
}
 
class List {
 
    char cmd;
    List next;
 
    public List() {
 
    }
 
    public void setcmd(char cmd) {
        this.cmd = cmd;
    }
 
    public void setnext(List next) {
        this.next = next;
    }
 
}
cs


-11.01 


역시나 시간초과가 뜬다. 양방향 링크리스트로 해결이 불가능한건가? 아니면 내가 뭔가 놓치고 있는 부분이 있을 것 같다 ㅠㅠ

스택으로 짜고 싶었지만 오기가 생겨서 계속 했는데...런타임에러를 잡으면 시간초과가 뜨고, 시간초과를 잡으면ㅋㅋㅋ 런타임에러가 자꾸 뜬다. ㅠㅠ 해결 못할 것 같다...흑


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
import java.util.Scanner;
 
public class Main {
 
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 명령 갯수
        for (int i = 0; i < n; i++) {
            String s = sc.next(); // 명령어
            LinkedList L = new LinkedList();
            for (int j = 0; j < s.length(); j++) {
                char cmd = s.charAt(j);
                if (cmd == '-') {
                    L.removeNode();
                } else if (cmd == '<') {
                    L.goprev();
                } else if (cmd == '>') {
                    L.gonext();
                } else {
                    L.addNode(cmd + "");
                }
            }
            L.viewNodes();
        }
    }
}
 
class LinkedList {
 
    Node head = new Node();
    Node cursor = head;
    int size = 0;
 
    public LinkedList() {
 
    }
 
 
    public void addNode(String cmd) {
 
        if (size == 0) {
            head.next = new Node(head, cmd);
            cursor = head.next;
        } else {
            Node tmp = new Node(cursor, cmd); // 새로 추가할 노드
            if (cursor.next != null) {
                tmp.next = cursor.next;
                cursor.next.prev = tmp; // 새로 추가할 노드를 next 에 넣어주고,
            }
            cursor.next = tmp;
            cursor = cursor.next;
        }
        size++;
    }
 
    public void goprev() {
        if (cursor != head) {
            cursor = cursor.prev;
        }
    }
 
    public void gonext() {
        if (cursor.next != null) {
            cursor = cursor.next;
        }
    }
 
    public void removeNode() {
        if (size == 0) {
 
        } else {
            if (cursor != head) {
                if (cursor.next != null) {
                    cursor.prev.next = cursor.next;
                    cursor.next.prev = cursor.prev;
                } else {
                    cursor.prev.next = null;
                }
                cursor = cursor.prev;
                size--;
            } else {
 
            }
        }
    }
 
    public void viewNodes() {
 
        Node c = head;
        String s = "";
        for (int i = 0; i < size; i++) {
            c = c.next;
            s += c.cmd;
        }
        System.out.println(s);
    }
 
    class Node {
 
        Node prev;
        String cmd;
        Node next;
 
        public Node() {
 
        }
 
        public Node(Node prev, String cmd) {
            this.prev = prev;
            this.cmd = cmd;
        }
 
    }
 
}
 
cs


'공부 > 알고리즘' 카테고리의 다른 글

백준2252_줄 세우기  (0) 2016.11.08
백준1620_나는야 포켓몬 마스터 이다솜  (1) 2016.11.04
백준1991_트리  (1) 2016.11.02
백준10845_큐  (1) 2016.10.28
백준2493_탑  (1) 2016.10.28

댓글