broken keyboard

Topic description:
You have a broken keyboard. All keys on the keyboard work fine, but sometimes the Home or End key is automatically pressed. You don't know the problem with the keyboard, and you focus on typing without even turning on the monitor. When you turn on the monitor, you are presented with a tragic text. Your task is to work out this tragic text before turning on the display.
Input Output: Input contains multiple sets of data. Each set of data occupies one line and contains no more than 100,000 letters, underscores, characters "[" or "]". The character "[" represents the Home key, and the character "]" represents the End key. The input end marker is the end-of-file character EOF. The input file should not exceed 5MB. For each set of data, output one line, which is the text of the tragedy on the screen.
Sample input and output:
insert image description here

Note: The code is derived from the classic algorithm competition entry (edited by Liu Rujia) P144, this article just adds some comments.
Attachment: Press the Home key to move the cursor to the beginning of the string, and press the End key to move the cursor to the end of the string.
Inscription: If you use an array to save, if you encounter Home or End and then insert characters and need to move other elements of the array, the amount of calculation may explode, so you can use a linked list, and you only need to change the successor to insert. s[i] is used to save the original initial string, next[i] is used to record the number of the next element of s[i] in the s array, that is, the successor, and cur is used to record the cursor, that is, the cursor is in s[cur ], last is used to record the position of the last character on the display.
Among them, the four lines of key code are relatively winding, and the reader can use the sample to go through it . The
code is as follows:

#include <iostream>
#include <cstdio>
#include<string.h>
const int maxn=10000+5;
using namespace std;

int cur,last;
int _next[maxn];
char s[maxn];

int main()
{
    
    
    while(scanf("%s",s+1)==1){
    
    
        int n=strlen(s+1); //数组s从下标为1开始存储,s[1]、s[2]....
        last=cur=0;
        _next[0]= 0;
        
        
        for(int i=1;i<=n;i++){
    
    
            char ch=s[i];
            if(ch=='[')   //遇到“[”就把光标移到最前面
                cur=0;
            else if (ch==']') {
    
          //遇到“]”就把光标移到最后面
                cur=last;
            }
            else {
    
    
                _next[i]=_next[cur];//在_next[i]中先保存光标左边元素在s中的编号
                _next[cur]=i;//此时读到了数组s中的第i个元素,光标左边的元素在显示屏中的下一个元素的编号就是i
                if(cur==last)
                    last=i;//更新显示屏上最后一个字符的编号
                cur=i;//移动光标
            }
        }
        //第一个显示屏上的元素的编号x在_next[0]中,由此知道s[x],下一个元素的编号在_next[x]中,以此类推
        for(int i=_next[0];i!=0;i=_next[i]) 
            printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472772&siteId=291194637