【日常学习】【非指针链表】Uva11988 - Broken Keyboard (a.k.a. Beiju Text)题解

这道题目拖了好几天,因为鄙人有两大天敌——链表和树TUT看了这个题材知道原来链表可以不用指针写,不过原理也是一样的,相当于是用数组模拟了个链表而不实用结构体,结构体里的指针就换成了两个变量cur和last了。这道题目本来测出来非常奇怪和合因为UVA AC HDU TLE SPOJ RE我正在奇怪,才发现同名的不同题目有三道TUT

题目的详解已经写在了注释里,上代码:

#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=100000+5;
int last,cur,next[maxn];
char s[maxn];

int main(){
	while (scanf("%s",s+1)==1){
		int n=strlen(s+1);//输入从s[1]开始存储
		last=cur=0;
		next[0]=0;//0位置的下一个数初始化为0 ,会改变
		for (int i=1;i<=n;i++){//按照字符的编号排列顺序输出字符,顺序是排列好的编号序列,该序列储存在next中
			char ch=s[i];
			if (ch=='[') cur=0;
			else if (ch==']') cur=last;
			else {
				next[i]=next[cur];//现在字符位置的下一个是当前光标的下一个【未插入之前当前光标下一个】 a|c
				next[cur]=i;//当前光标的下一个是现在插入的字符位置,即此时光标还是在原来的前一个字符后面 a|bc
				if (cur==last) last=i;//如果当前已是最后,最后编号更新 last指向最后一个 next[last]为空 先比较再移动cur
				cur=i;//移动光标到当前插入字符后面 ab|c
			}
		}
		for (int i=next[0];i!=0;i=next[i]) printf("%c",s[i]);//注意占位符,另外其实next数组应该初始化为0
		printf("\n");
	}
	return 0;
}

——高祖尝徭咸阳,纵观,观始皇帝,喟然太息曰:“嗟乎,大丈夫当如此也!”

时间: 2024-12-14 18:05:18

【日常学习】【非指针链表】Uva11988 - Broken Keyboard (a.k.a. Beiju Text)题解的相关文章

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

刘汝佳的题目,悲剧文本 -_-||| 这里使用vector<string>容器倒置记录数据,然后从后面输出就可以了. 难度就是不知道这样的文档到底哪里是开始输出,故此使用动态管理内存的容器比较好做. 增加了io处理的O(n)算法也没有上榜,郁闷. #include <stdio.h> #include <vector> #include <string> using std::vector; using std::string; const int MAX_

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automatically pressed (inter

Uva11988 Broken Keyboard (a.k.a. Beiju Text)(就是先输出括号的字符)

Description Problem B Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets automa

UVA11988 Broken Keyboard (a.k.a. Beiju Text)

看到大一练习题,睡前水一水~~~ Problem B Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "home" key or the "end" key gets a

UVA-11988 Broken Keyboard (a.k.a. Beiju Text) (链表 或 递归)

题目大意:将一个字符串改变顺序后输出.遇到“[”就将后面内容拿到最前面输出,遇到“]”就将后面的内容拿到最后面输出. 题目分析:用nxt[i]数组表示i后面的字符的下标,实际上就是以字符i为头建立链表,写法类似链式前向星. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; char p[100005]

UVa11988 Broken Keyboard (a.k.a. Beiju Text) (链表)

链接:http://acm.hust.edu.cn/vjudge/problem/18693分析:链表.用nxt[i]保存s[i]右边字符在s中的下标.增加一个虚拟字符s[0],那么nxt[0]就是显示屏中最左边的字符.cur表示光标位置左边的字符在s数组中的下标,last表示显示屏中最后一个字符在s数组中的下标.其实就是用nxt链表重新组织s数组的输出顺序,输入一个字符,先将新插入字符的nxt[i]指向nxt[cur],再将nxt[cur]指向i,就是在链表元素cur后插入元素i,更新最后一个

uva - Broken Keyboard (a.k.a. Beiju Text)(链表)

11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problemwith the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed(internally).Yo

Broken Keyboard (a.k.a. Beiju Text) UVA, 11988(链表)

Broken Keyboard (a.k.a. Beiju Text) From:UVA, 11988 Time Limit: 1000 MS Broken Keyboard (a.k.a. Beiju Text) You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem with the keyboard is that sometimes the "hom

N - Broken Keyboard (a.k.a. Beiju Text)

N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem with the keyboard is that som