首页 > 其他分享 >链表

链表

时间:2023-07-01 20:13:10浏览次数:49  
标签:prev int tot next 链表 now

题目:

小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;
}

标签:prev,int,tot,next,链表,now
From: https://www.cnblogs.com/bzlx1717/p/17519843.html

相关文章

  • LeetCoe-25-K个一组翻转链表
    25题:K个一组翻转链表题目给你链表的头节点head,每 k 个节点一组进行翻转,请你返回修改后的链表。k是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换......
  • 算法学习day03链表part01-203、707、206
    packageSecondBrush.LinkedList.LL1;/***203.移除链表元素*删除链表中等于给定值val的所有节点。*自己再次概述一下这个过程:*1.移除元素,要采用设置虚拟节点的方式,因为那样不需要考虑头结点问题*2.设置两个虚拟指向*3.移除元素就是遍历链表,然后碰到目标值......
  • 算法学习day04链表part02-24、19、0207、142
    packageSecondBrush.LinkedList.LL1;/***24.两两交换链表中的节点**/publicclassSwapNodesInPairs_24{publicListNodeswapPairs(ListNodehead){ListNodedummyhead=newListNode(-1);dummyhead.next=head;ListNodecur......
  • 【leetcode】【234】【回文链表】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......
  • [刷题记录Day3]Leetcode链表专题
    #ListNodedefinitionpublicclassListNode{//结点的值intval;//下一个结点ListNodenext;//节点的构造函数(无参)publicListNode(){}//节点的构造函数(有一个参数)publicListNode(intval){this.val=val;......
  • 【leetcode】【206】【反转链表】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......
  • js 数组和链表分别实现队列
    链表实现/***1.单项链表实现队列,但要同时记录head和tail*2.要从tail入队,head出对,否则出队时,tail不好定位*2.单独记录length,不可遍历链表获取length*/classMyQueue{head=null;//头tail=null;//尾len=0;add(n){letnewNode={......
  • 【leetcode】【83】【移除链表元素】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......
  • LeetCode 142. 环形链表 II
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*detectCycle(ListNode*head){if(!head)return......
  • leetcode 19. 删除链表的倒数第 N 个结点
    链表问题,需要注意一下是倒着数还是正着数,和头结点会不会被删除即可publicListNoderemoveNthFromEnd(ListNodehead,intn){if(head==null){returnnull;}//头结点会被删除吗?intlength=0;ListNodep=......