-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1406.rb
63 lines (52 loc) · 1015 Bytes
/
1406.rb
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
class Node
attr_accessor :left, :right, :letter
end
str = gets.strip
head = Node.new
tail = head
str.each_char do |c|
tmp = Node.new
tmp.letter = c
tmp.left = tail
tail.right = tmp
tail = tmp
end
cursor = tail
n = gets.to_i
n.times do
command, letter = gets.split(' ')
case command
when 'L'
cursor = cursor.left if cursor.letter
when 'D'
cursor = cursor.right if cursor.right
when 'B'
if cursor.letter.nil?
next
elsif !cursor.right
cursor.left.right = nil
cursor = cursor.left
else
cursor.left.right = cursor.right
cursor.right.left = cursor.left
cursor = cursor.left
end
when 'P'
tmp = Node.new
tmp.letter = letter
if cursor.right
cursor.right.left = tmp
tmp.right = cursor.right
end
tmp.left = cursor
cursor.right = tmp
cursor = tmp
end
end
cursor = cursor.left while cursor.letter
str = ''
while cursor.right
str << cursor.right.letter
cursor = cursor.right
end
puts str