PTA Xiaoming Typing (10 points) (data structure + doubly linked list)

7-1 Xiao Ming Typing (10 points)

Xiao Ming is typing a document using Microsoft Word. The document only contains the lowercase letters of az and spaces. During typing, he may press the Home key, End key, ← arrow keys, → arrow keys, Insert key, Backspace one or more times. key. Please write a program, given the sequence of Xiao Ming's keystrokes on the keyboard, output the final text displayed on Xiao Ming's screen. Tip: The Home key will move the current cursor to the beginning of the text, the End key will move the current cursor to the end of the text, the ← and → keys will move the current cursor one position to the left or right (if the cursor is at the beginning of the document, it cannot be moved left. The cursor cannot be moved right at the end of the document), the Insert key will switch between inserting and replacing text (the default is the insert state), and the Backspace key will delete a character before the current cursor.

Input format:

The input is no more than 50,000 characters, which means Xiao Ming's key sequence. Contains az lowercase letters, spaces, and the characters [,], {,}, -, =. The character "[" represents the Home key, "]" represents the End key, "{" represents the ← key, "}" represents the → key, "-" represents the Insert key, and "=" represents the Backspace key.

Output format:

The output is the final text displayed on Xiao Ming's screen. There is no carriage return or line feed after the last letter.

Sample

Input example 1:

jilin[i lofe{
    
    {
    
    -v-} ] universiti=y

Output example 1:

i love jilin university

Input example 2:

abcd[c-de

Output example 2:

cdecd

Input example 3:

[[]][][]happy=birthday

Output sample 3:

happbirthday

Input example 4:

efg[bbb}}=}}}}=[{
    
    {
    
    {
    
    {
    
    a

Output sample 4:

abbbe

limit

Author: Zhuyun Gang
Unit: Jilin University
code size limit: 16 KB
Time limit: 25 ms
Memory Limit: 10 MB

Problem solving

The time limit for the question is 25 milliseconds. Investigate the doubly linked list of the data structure. During the problem-solving process, pay attention to the deletion and insertion of the doubly linked list (pay special attention to the deletion and insertion of the first and last positions).
This question uses a linked list with empty nodes to facilitate the insertion and deletion at the head of the linked list. operating

Code

#include <algorithm>  //7-1 小明打字 (10分)
#include <cstring>
#include <iostream>
using namespace std;

typedef struct LNode {
    
    
    char ch;
    LNode* next;
    LNode* prev;
} * List;

int main() {
    
    
    char ch;
    List start, end, tmp;  //带空头节点链表, 每次插入tmp后面
    List L = new LNode();
    start = L;
    end = L;
    tmp = L;

    bool isInsert = true;

    List ins;
    while (scanf("%c", &ch) && ch != '\n') {
    
    
        if (ch == '[') {
    
    
            tmp = start;
        } else if (ch == ']') {
    
    
            tmp = end;
        } else if (ch == '{') {
    
    
            if (tmp->prev) tmp = tmp->prev;
        } else if (ch == '}') {
    
    
            if (tmp->next) tmp = tmp->next;
        } else if (ch == '-') {
    
    
            isInsert = !isInsert;
        } else if (ch == '=') {
    
      //删除tmp
            if (tmp->prev) {
    
    
                List t = tmp;
                tmp->prev->next = tmp->next;
                if (tmp->next) {
    
    
                    tmp->next->prev = tmp->prev;
                    tmp = tmp->prev;
                } else {
    
    
                    tmp = tmp->prev;
                    tmp->next = NULL;
                }
                delete t;
                if (tmp->next == NULL) end = tmp;  //如果是删除最后的, 更新end
            }
        } else {
    
      // tmp后面添加
            ins = new LNode();
            ins->ch = ch;
            ins->next = tmp->next;
            ins->prev = tmp;
            tmp->next = ins;
            tmp = ins;
            if (tmp->next == NULL) end = tmp;  //如果插入在最后, 更新end
            if (!isInsert && tmp->next) {
    
      //如果是替换, 且tmp下一个存在, 再删除tmp下一个
                List t = tmp->next;
                if (t->next) {
    
    
                    tmp->next = t->next;
                    t->next->prev = tmp;
                    delete t;
                } else {
    
    	//删除元素在末尾
                    delete t;
                    end = tmp;  //更新end
                    tmp->next = NULL;
                }
            }
        }
        // end->next = NULL;	//保险, 但没必要
    }

    tmp = start->next;
    while (tmp) {
    
    
        printf("%c", tmp->ch);
        tmp = tmp->next;
    }
    // 注意清理内存, 这里节省运行时间不清理

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45349225/article/details/109633790