链表

题目:

小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

代码

#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;
}
posted @ 2023-07-01 19:52  本征粒向  阅读(95)  评论(0)    收藏  举报