2024-04-21 23:59:39

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static StringBuilder sb = new StringBuilder();
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
            _1406_boj boj = new _1406_boj();
 
            boj.boj_1406();
 
        }
    }
        public class Node
    {
        public char data;
        public Node link = null;
    }
    internal class _1406_boj
    {
        public static Node head = null;
        public StringBuilder sb = new StringBuilder();
        public void boj_1406()
        {
            string input = Console.ReadLine();
            for (int i =0; i < input.Length; i++)
            {
                make_node(input[i]);
            }
            make_node('C');
            print_node(head);
            int cnt = int.Parse(Console.ReadLine());
            for (int i = 0; i < cnt; i++)
            {
                string input2 = Console.ReadLine();
                switch(input2[0])
                {
                    case 'P':
                        insert_node(input2[2]);
                        break;
                    case 'L':
                        move_left();
                        break;
                    case 'D':
                        move_right();
                        break;
                    case 'B':
                        delete_node();
                        break;
                }
                print_node(head);
            }
        }
        public void make_node(char data)
        {
            Node node = new Node();
            node.data = data;
            if (head == null)
            {
                head = node;
                return;
            }
 
            Node current = head;
            while (current.link != null)
                current = current.link;
            current.link = node;
 
        }
        public void print_node(Node start)
        {
            if (head == null)
                return;
            if (start.data != 'C')
                sb.Append(start.data);
            while (start.link != null)
            {
                start = start.link;
                if (start.data != 'C')
                    sb.Append(start.data);
 
            }
            Console.WriteLine(sb);
            sb.Clear();
        }
        public void insert_node(char insert_data)
        {
            Node node = new Node();
            node.data = insert_data;
 
            if (head.data == 'C')
            {
                node.link = head;
                head = node;
            }
 
 
            Node current = head;
            while (current.link != null)
            {
                Node pre = current;
                current = current.link;
                if (current.data == 'C')
                {
                    pre.link = node;
                    node.link = current;
                }
            }
        }
        public void move_left()
        {
            Node current = head;
            Node node = new Node();
            node.data = 'C';
            if (head.data == 'C')
            {
                return;
            }
            if (head.link.data == 'C')
            {
                node.link = head;
                head.link = head.link.link;
                head = node;
                return;
            }
 
            while (current.link != null)
            {
                Node pre = current;
                current = current.link;
                if (current.link.data == 'C')
                {
 
                    node.link = current;
                    pre.link = node;
                    if (current.link.link == null)
                        current.link = null;
                    else
                        current.link = current.link.link;
                    return;
                }
 
            }
        }
        public void move_right()
        {
            Node current = head;
            Node node = new Node();
            node.data = 'C';
            if (head.data == 'C')
            {
                head = head.link;
                node.link = head.link;
                head.link = node;
                current.link = null;
                return;
            }
            while (current.link != null)
            {
                Node pre = current;
                current = current.link;
 
                if (current.data == 'C')
                {
                    if (current.link == null)
                        return;
                    else
                    {
                        pre.link = current.link;
                        node.link = current.link.link;
                        current.link.link = node;
                        current.link = null;
                        return;
                    }
                }
 
            }
        }
        public void delete_node()
        {
            Node current = head;
            if (head.data == 'C')
                return;
            while (current.link != null)
            {
                Node pre = current;
                current = current.link;
                if (current.data == 'C')
                {
                    if (pre == head)
                    {
                        pre.link = null;
                        head = current;
                        return;
                    }
                    pre.link = current.link;
                    current.link = null;
                    pre.data = 'C';
                    return;
                }
            }
        }
    }
}
 
cs

시간초과가 계속 뜬다... 다른 방법을 찾아봐야겠지?