Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdio.h>
#include <list>
#include <string>
using namespace std;

int main(void)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int main(void)
int main()

{
char origin[100001];
list<char> lst;
int M;

scanf("%s", origin);
for (int i = 0; origin[i]; i++)
lst.push_back(origin[i]);

scanf("%d", &M);
list<char>::iterator it = lst.end();
while (M--)
{
char cmd;
scanf(" %c", &cmd);
if (cmd == 'L')
{
if (it != lst.begin())
it--;
}
else if (cmd == 'D')
{
if (it != lst.end())
it++;
}
else if (cmd == 'B')
{
if (it != lst.begin())
{
it--;
it = lst.erase(it);
}
}
else if (cmd == 'P')
{
char c;
scanf(" %c", &c);
lst.insert(it, c);
}
}

string result(lst.begin(), lst.end());
printf("%s\n", result.c_str());
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
input = sys.stdin.readline

origin = input().rstrip()
left_lst = list(origin)
right_lst = []
M = int(input())
while M > 0:
cmd = input().rstrip()
if cmd[0] == 'L':
if left_lst:
right_lst.append(left_lst.pop())
elif cmd[0] == 'D':
if right_lst:
left_lst.append(right_lst.pop())
elif cmd[0] == 'B':
if left_lst:
left_lst.pop()
elif cmd[0] == 'P':
left_lst.append(cmd[2])
M -= 1
Comment on lines +8 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while M > 0:
cmd = input().rstrip()
if cmd[0] == 'L':
if left_lst:
right_lst.append(left_lst.pop())
elif cmd[0] == 'D':
if right_lst:
left_lst.append(right_lst.pop())
elif cmd[0] == 'B':
if left_lst:
left_lst.pop()
elif cmd[0] == 'P':
left_lst.append(cmd[2])
M -= 1
for _ in range(M):
cmd = input().rstrip()
if cmd[0] == 'L':
if left_lst:
right_lst.append(left_lst.pop())
elif cmd[0] == 'D':
if right_lst:
left_lst.append(right_lst.pop())
elif cmd[0] == 'B':
if left_lst:
left_lst.pop()
elif cmd[0] == 'P':
left_lst.append(cmd[2])


print(''.join(left_lst + right_lst[::-1]))