# | Problem | Pass Rate (passed user / total user) |
---|---|---|
12668 | Text Editor - Linked List Version |
|
13442 | Lucky money 2 |
|
14221 | Band Ya Rou Ze - Reverse (revisited) |
|
14576 | Tortoise and Hare |
|
Description
Modified from 10389 - text editor
In this problem we simulate a simple text editor. Given a series of keyboard input, output the final text content. Initially, the text content is empty and the cursor is at the beginning of the line. The user can:
-
Insert a lower-case alphabet ('a-z') before the cursor, denoted as a lower-case alphabet.
-
Erase an alphabet before the cursor, denoted as
B
. -
Move the cursor to the right one character at a time, denoted as
R
. -
Move the cursor to the left one word at a time, denoted as
L
.
Hints
In this problem, you are asked to use doubly linked list to reduce the time complexity. Three typical implementations as follows are possible. You can choose your favorite one, e.g., Impl 1 or Impl 2.
Remember to free memory when a linked list node is deleted. If you get Runtime Error
on test case, probably there is something wrong with the pointers or there is memory leak in your program.
Make sure the time complexity is O(1) for each operation, otherwise you may get Time limit exceed
Input
The first line is an integer T (1 <= T <= 50), meaning that there are T subtasks in the input.
In each subtask, there are two lines. The first line is an integer N(1<= N <= 106), being the length of the series of keyboard input. The next line is a string of length N, being the series of keyboard input.
It is guaranteed that L
and B
will not occur when the cursor is in the left-most position, R
will not occur when the cursor is in the right-most position.
Output
For each subtask, output the final text content in a single line.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Receiving lucky money is the favorite activity of children during Chinese New Year.
Denny's family has N children, everyone wants to get the lucky money as soon as possible.
The elders in the family have prepared a game to decide the order of receiving the lucky money:
- At the beginning, all children sit in a circle, with seats numbered from 1 to N.
- Randomly decide two numbers A and K and the direction R/L, move the Ath youngest child K positions clockwise/counterclockwise.
For example:
N = 6
The given initial age sequence: 1 1 3 4 5 5
A = 3, K = 2, R
After processing:
A = 1, K = 3, L
After processing:
A = 1, K = 6, R
After processing:
A = 2, K = 2, R
After processing:
Output:
The age sequence in clockwise from the youngest: 1 3 5 4 5 1
Note: if two children have same age, consider the children with less order yonger.
Input
The first line contains two integers N M, the number of children and the number of position changes.
The second line contains N integers, giving the children's age sequence.
Each of the following M lines contains two integers A and K and the direction 'R' or 'L'.
test cases:
Output
The output contains one line.
Output the age sequence of the children in clockwise from the youngest.
Sample Input Download
Sample Output Download
Partial Judge Code
13442.cPartial Judge Header
13442.hTags
Discuss
Description
After catching Suicune, Spongebob and Patrick thought they were happy enough so they went home. And then they found that Squidward had come home already (:
Squidward told them he need to drum up a marching band fast(for some reason). So he needed their help. Hence Spongebob and Patrick joined Squidward's band.
Today is their practicing day. Squidward is teaching all the members how to move at a performance. At a performance there are N rows. And in the i-th row, there are szi people. Note row may be empty(i.e. szi = 0). Everyone plays exactly one musical instrument. The musical instrument played by the j-th people in the i-th row is represented by a lower case letter cij.
Squidward ask them to move Q times. In each move, there are four types of moving:
- all the people in the ai-th row move to the front of the bi-th row (without messing up the original order)
- all the people in the ai-th row move to the back of the bi-th row (without messing up the original order)
- exchange all the people in the ai-th row and the bi-th row (without messing up the original order)
- reverse the order of all the people in the ai-th row
Now it’s your turn! You need to help them to move and tell Squidward the final formation in the end.
Hint for reverse:
1. Easy way , but you can only get 2 more correct answers.
But, you should think another way to accelerate your performance to get AC.
Hint:
If you have no idea how to implement, please take it as your reference.
#include <stdlib.h> #include <stdio.h> typedef struct _Node { char val; struct _Node* next; } Node; //list[i]'s head node Node *head[100005] = {}; //list[i]'s tail node Node *tail[100005] = {}; //reverse(list[i])'s head node Node *rev_head[100005] = {}; //reverse(list[i])'s tail node Node *rev_tail[100005] = {}; /* list[a] = 1 -> 2 -> 3 -> 4 rev_list[a] = 4 -> 3 -> 2 -> 1 head[a]->val = 1, tail[a]->val = 4 rev_head[a]->val = 4, rev_tail[a]->val = 1 you can use rev_head and rev_tail to get O(1) reverse */ void swap(int a, int b) { //swap list[a] and list[b] Node *tmp = (Node *)malloc(sizeof(Node)); /* swap(head_node) tmp = head[a]; head[a] = head[b]; head[b] = tmp; */ /* swap(tail_node) */ /*swap(rev_head) */ /*swap(rev_tail) */ } void append(int a, int b) { //append list[a] to list[b]'s behind if(head[a] == NULL) return; if(head[b] == NULL) { swap(a, b); return; } /* tail[b]->next = head[a]; tail[b] = ... ... */ } void reverse(int a) { //reverse list[a] if(head[a] == NULL) return; /* use rev_head and rev_tail to reverse list[a] in O(1) hint: swap something */ }
Input
The first line contains one integer N (1 ≤ N ≤ 105) – the number of rows.
Then N lines follow. First the i+1-th line contains one integer szi – the number of people in the i-th row. If there are some people in the row, a space character and szi characters cij are followed where cij is the musical instrument played by the j-th people in the i-th row. The sum of all the szi is less than or equal to 106.
The N+2-th line contains one integer Q (1 ≤ Q ≤ 105) – the times they need to move.
Then Q lines follow. The i+N+3-th line contains two integer typei, ai. If typei isn't equal to 4, then a integer bi follows(1 ≤ typei ≤ 4, 1 ≤ ai, bi ≤ N, ai ≠ bi) – they are the corresponding information for each moving.
It's guaranteed that:
- The 1st testcase must be identical to the Sample #1 below
- For the first three testcases: 1 ≤ N, Q ≤ 103, the sum of all the szi ≤ 104, all the typei ≠ 4
- For the first six testcases: all the typei ≠ 4
- For the first eight testcases: the number of the fourth type of moving ≤ 100
Output
After all the moving, for each row output all the musical instrument played in that row and then print a newline('\n') character.
If there is no people in some row, just output an empty line.
Note: there are two sample below. "# Sample Input 1/2" and "# Sample Output 1/2" are not the part of input and output.
They are just for marking that the following content corresponds to which sample.
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Given a linked list of length N, where the last node points to the M-th node in the list. Note that when M equals 0, it indicates that the last node points to NULL.
Your task is to get an input T and perform T insertion operations.
For each operation, you need to find the first node with value A (where V = A) in the list and insert a new node with value B after it. If no node with value A is found in the list, skip this operation.
After completing all operations, output each node's value in the linked list once, starting from the head.
Example Operation 1:
Insert a node with value 2 after the node with value 1.
Example Operation 2:
Insert a node with value 6 after the node with value 5.
Other Example Operation:
This question is a partial judge.
Please implement functions defined in the header file.
- insertNode: get an input T and perform T insertion operations
- printLinkedList: output each node's value in the linked list once, starting from the head
Input
The first line contains two integers N and M, where N represents the length of the linked list, and the last node in the linked list points to the M-th node.
The second line contains N integers Vi , representing the value of the i-th node.
The third line contains one integer T, representing T insertion operations to be performed.
The following T lines each contain two integers A and B, indicating to insert a node with value B after the first node with value V equal to A.
Constraints
- 1 ≤ N ≤ 106
- 0 ≤ M ≤ N
- 1 ≤ T ≤ 100
- -109 ≤ A, B, V ≤ 109
Subtasks
- Testcases 1 ~ 3: 0 ≤ V ≤ 106, V is unique, M = 0
- Testcases 4 ~ 6: 0 ≤ V ≤ 106, V is unique
- Testcases 7 ~ 8: No additional restrictions
Output
After completing T operations, output each node's value in the linked list once, starting from the head.
Please remember to print "\n" at the end.