11316 - Super Text Editor   

Description

In this problem we simulate a text editor. Given a series of keyboard input, output the final text content.

The text editing rules are defined as follows:

  1. Normal alphabetical input and whitespace input (abcdefg…. and ‘ ‘): directly write after the cursor of the text content.
  2. Eight special commands started with a backslash(/) character
  1. The backspace command, /b: deletes a letter before the cursor.
  2. The newline command, /n: creates a new line after the cursor.
  3. The three navigating commands, /u, /l, and /r: move the cursor in the corresponding direction up, left, and right.
  • Note that /u moves the cursor to the same position at the previous line. If the new position exceeds the end of the line, the cursor should be moved to the end of the line.
  1. The sort command, /s: sorts a portion of the string in lexicographical order. The range starts from the cursor and ends at the next newline character. (Exclude the newline character.) Note that when doing sorting, the cursor should not move.
  2. The copy command, /c: copies a portion of the string. The range starts from the cursor and ends at the next newline character. (Exclude the newline character.)
  3. The paste command, /p: pastes the string that had been copied at the cursor.

 

The size of the text content is fixed to 500 characters, and the text content of test cases will not exceed 500 characters when simulating.

 

Example 1:

Input:

aaaaa/naaaaa/naaa/l/ubbbccccc/u/u/uddd

Output:

aaaaaddd
aabbbcccccaaa
aaa

Example 2:

Input:

edcba/c/l/l/l/p/s/c/p/pzzz

Output:

edabcabczzzabc

Hint:

The 1st test case includes “/l”, “/s”.

The 2nd test case includes “/r”, “/l”, “/n”, “/b”.

The 3rd and 4th test cases include “/s”, “/r”, “/l”, “/n”, “/b”.

The 5th and 6th test cases include “/s”, “/r”, “/l”, “/n”, “/b”, “/c”, “/p”.

The 7th and 8th test cases include “/s”, “/r”, “/l”, “/n”, “/b”, “/c”, “/p”, “/u”.

 

Hint2: You can use the following code to read the input.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_SIZE 501

char input[MAX_SIZE] = {0};

​// It will be easier to manipulate the string in a 1D character array
char content[MAX_SIZE] = {0};

int main() {
    gets(input);

    int i;
    for (i = 0; i < MAX_SIZE; i++) {
        if (input[i] == '/') {
            if (input[i + 1] == 'b') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'n') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'l') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'r') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'u') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'c') {
                i++;
                // Your code here
            } else if (input[i + 1] == 'p') {
                i++;
                // Your code here
            } else if (input[i + 1] == 's') {
                i++;
                // Your code here
            }
        } else if (isalpha(input[i]) || input[i] == ' ') {
            // Your code here
        }
    }

    printf("%s", content);

    return 0;
}

Input

The keyboard input sequence.

There is always a valid command(/b /n /l /r /u /c /p /s) right after the backslash character.

There is no newline character at the end.

Output

The final text content.

Sample Input  Download

Sample Output  Download

Tags




Discuss