链表

发布时间 2023-07-01 19:56:22作者: 本征粒向
题目:

小A打字时有不看屏幕的习惯。在一次小A打字时,调皮的小B常常趁小A不注意按下Home键和End键。当Home​键被按下时,
输入光标会跳到文本最开头;当End键被按下时,输入光标会跳到文本末尾。现给出若干行按键的字符串,其中'['表示Home键,']'表示End键,
其余字符均表示输入的内容,每行字符串长度不超过100000,问最终显示的文本是什么。
输入格式
多组数据,处理到EOF为止
每组数据占一行,是一个长度不超过100000​、只包含大小写字母、下划线以及'['和‘]’的字符串。
输出格式
对于每组数据,输出一行最终显示的字符串。
输入样例
This_is_a_[simple_sample]_text
[][][[]][]Good_luck_to_you
abcd[efghi]jklm[nopq]rs[t]uvwxyz
输出样例
simple_sampleThis_is_a__text
Good_luck_to_you
tnopqefghiabcdjklmrsuvwxyz

```cpp

#include<bits/stdc++.h>
using namespace std;
int head, tail, tot;
struct Node {
char value;
int next, prev;
} a[100010];
void Init() {
tot = 2;
head = 1, tail = 2;
a[1].next = 2;
a[2].prev = 1;
}
void Insert(char value, int x) {
int now = ++tot;
a[now].value = value;
a[a[x].next].prev = now;
a[now].next = a[x].next;
a[x].next = now;
a[now].prev = x;
}
int main() {
string s;
int now;
while (cin >> s) {
Init();
now = 1;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '[' || s[i] == ']') {
if (s[i] == '[') {
now=1;
}
else{
now=a[2].prev;
}
} else {
Insert(s[i], now);
now = tot;
}
}
for(int i=a[1].next;i!=2;i=a[i].next){
cout<<a[i].value;
}
cout<<endl;
}
return 0;
}