-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProblemB.cpp
117 lines (108 loc) · 2.58 KB
/
ProblemB.cpp
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
#include<cstdio>
#include<cstdlib>
char s[100005];
class Node {
public:
int val;
Node *next;
Node *prev;
};
class List {
public:
Node *head;
Node *cursor;
Node *tail;
List();
void insert(int val);
void left();
void right();
void remove();
void traverse();
};
List::List() {
Node *p = new Node;
p -> val = -1;
p -> prev = nullptr;
p -> next = nullptr;
this -> head = p;
this -> tail = p;
this -> cursor = p;
}
void List::insert(int val) {
Node *p = new Node;
p -> val = val;
p -> next = this -> cursor;
if (cursor == head) {
this -> head = p;
p -> prev = nullptr;
} else {
p -> prev = this -> cursor -> prev;
this -> cursor -> prev -> next = p;
}
this -> cursor -> prev = p;
}
void List::left() {
if(cursor -> prev) this -> cursor = cursor -> prev;
}
void List::right() {
if(cursor -> next) this -> cursor = cursor -> next;
}
void List::remove() {
if(this -> cursor -> val == -1) return;
Node *p = this -> cursor;
if(p == this -> head) {
this -> head = this -> head -> next;
this -> head -> prev = nullptr;
} else {
p -> prev -> next = p -> next;
if(p -> next) p -> next -> prev = p -> prev;
}
right();
delete p;
}
void List::traverse() {
Node *p = this -> head;
while(p) {
if(p -> val != -1) printf("%c", p -> val);
p = p -> next;
}
printf("\n");
}
int main() {
int T; scanf("%d", &T);
for(int t = 0; t < T; t++) {
List list;
int n; scanf("%d", &n);
scanf("%s", s);
for(int i = 0; i < n; i++) {
// list.traverse();
switch (s[i]) {
case 'r':
if(i < n - 1) {
if (list.cursor->val == -1) {
list.insert(s[++i]);
list.left();
} else list.cursor -> val = s[++i];
}
break;
case 'I':
list.cursor = list.head;
break;
case 'H':
list.left();
break;
case 'L':
list.right();
break;
case 'x':
list.remove();
break;
default:
list.insert(s[i]);
break;
}
}
list.traverse();
}
return 0;
}