์ฐธ๊ณ :https://ferrante.tistory.com/29
#include<iostream>
#include<string>
using namespace std;
class Node {
public:
char data;
Node* prev;
Node* next;
Node(char e) {
this->data = e;
this->prev = NULL;
this->next = NULL;
}
};
class Editor {
public:
int size;
Node* head;
Node* tail;
Node* cur;
Editor() {
size = 0;
head = new Node(1);
tail = new Node(1);
head->prev = NULL;
tail->next = NULL;
cur = tail;
}
void insert(char X) {
Node* newNode = new Node(X);
if (size == 0) {
newNode->next = tail;
newNode->prev = head;
head->next = newNode;
tail->prev = newNode;
}
else {
Node* tmp = cur->prev;
newNode->next = cur;
newNode->prev = tmp;
tmp->next = newNode;
cur->prev = newNode;
}
size++;
}
void remove() {
if (cur->prev != head) {
Node* tmp = cur->prev;
cur->prev = tmp->prev;
tmp->prev->next = cur;
delete tmp;
}
}
void moveLeft() {
if (cur->prev != head) {
cur = cur->prev;
}
}
void moveRight() {
if (cur->next != NULL) {
cur = cur->next;
}
}
void print() {
Node* tmp = head->next;
while (tmp != tail) {
cout << tmp->data;
tmp = tmp->next;
}
cout << endl;
}
};
int main() {
Editor e;
string str;
int n;
char m;
cin >> str;
for (int i = 0; i < str.length(); i++) {
e.insert(str[i]);
}
cin >> n;
while (n--) {
cin >> m;
if (m == 'P') {
cin >> m;
e.insert(m);
}
else if (m == 'L') {
e.moveLeft();
}
else if (m == 'D') {
e.moveRight();
}
else {
e.remove();
}
}
e.print();
}
Verify vps by using Stack
#include<iostream>
#include<string>
using namespace std;
class Node {
public:
char data;
Node* next;
Node(char e) {
this->data = e;
this->next = NULL;
}
};
class Stack {
private:
Node* top;
int size;
public:
Stack() {
top = NULL;
size = 0;
}
bool empty() {
return(size == 0);
}
void push(char X) {
Node* newNode = new Node(X);
if (size == 0) {
top = newNode;
}
else {
newNode->next = top;
top = newNode;
}
size++;
}
void pop() {
if (size != 0) {
Node* temp = top;
top = top->next;
delete temp;
size--;
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
Stack stack;
while (N--) {
string ps;
cin >> ps;
bool isVps = true;
for (int i = 0; i < ps.length(); i++) {
if (ps[i] == '(')stack.push(ps[i]);
else if (!stack.empty() && ps[i] == ')')stack.pop();
else { isVps = false; break; }
}
if (!stack.empty()) { isVps = false; }
if (isVps)cout << "YES" << endl;
else cout << "NO" << endl;
while (!stack.empty())stack.pop();
}
}