Broken Keyboard(悲剧文本)

你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下。你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源。当你打开显示器之后,展现在你面前的是一段悲剧文本。你的任务是根据给出的键盘上实际输入的内容,计算并输出这段悲剧文本(即显示器上展现的文本)

Input:

一行文本不超过1000000个字符,只包括字母、下划线、字符‘[‘(表示Home键)和字符‘]‘(表示End键盘)。

Output:

一个字符串,即符合题目描述的悲剧文本。

Example:

Input:

This_is_a_[Beijing]_tex

Output:

BeijingThis_is_a__text

___________________________________________________________________________________________________________

问题分析:

该问题最简单的想法是利用数组来保存这些数据,然后用一个变量来保存光标位置,输入一个字符相当于在数组中插入一个字符,但是没插入一个字符将要移动大量的数据,程序的开销会很大。

为了解决这个问题,我们可以利用静态链表解决这个问题

先用一个字符串将每个字符储存起来,s[1-n],对每个s[i]都会对应一个next[i]游标,储存的是下一个结点的下标,同时利用一个curr,表示光标位置,并用last记录最后元素位置,s[0]是一个虚拟字符,用作头结点。

#include<iostream>
#include<string>
using namespace std;

const int size=100000+1;

int last;       //显示屏上的最后一个字符
int cur;       //光标位置,总在当前s[i]的左方
char s[size];             //用以储存string

int main()
{
	int next[size]={0};           //每个s[i]的游标,初始化为0
	while(scanf("%s",s+1)==1)
	{
		//初始化
		int n=strlen(s+1); //真正的字符从s[1]开始,s[0]是一个虚拟字符
		last=0;
	    cur=0;

		//重排next
		for(int i=1;i<=n;i++)
		{
			if(s[i]==‘[‘)
				cur=0;      //home键
            else if(s[i]==‘]‘)
                cur=last;				//end键
     	    else
			{
				next[i]=next[cur];
				next[cur]=i;
			    if(cur==last)last=i;   //当从string的最后插入时,last要+1
				cur=i;
			}
		}

	    //输出
		for(int j=next[0];j!=0;j=next[j])  //依据next中指定的顺序输出
			cout<<s[j];
		cout<<endl;
	}

	return 0;
}

  

时间: 2024-11-09 01:10:47

Broken Keyboard(悲剧文本)的相关文章

UVa11988 Broken Keyboard(悲剧文本)

UVa11988 Broken Keyboard(悲剧文本) 题目链接:UVa11988 题目描述: 输入包含多组数据,每组数据占一行,包含不超过100000个字母.下划线.字符"["或者"]".其中字符"["表示Home键,"]"表示End键.输入结束标志为文件结束符(EOF)输入文件不超过5MB,对于每组数据,输出一行,即屏幕上的悲剧文本 样例输入: This_is_a_[Beiju]_text [[]][]Happy_B

Broken Keyboard--又名悲剧文本(线性表)

 题目: 你有一个破损的键盘.键盘上的所有键都可以正常工作,但有时Home键或者End键会自 动按下.你并不知道键盘存在这一问题,而是专心地打稿子,甚至连显示器都没打开.当你 打开显示器之后, 展现在你面前的是一段悲剧的文本.你的任务是在打开显示器之前计算出 这段悲剧文本. 输入包含多组数据.每组数据占一行,包含不超过100000个字母.下划线.字符"["或 者"]".其中字符"["表示Home键, "]"表示End键.输入结

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

11988 - 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

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_

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

UVa11988:Broken Keyboard

Description Broken KeyboardYou'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 gets automatically pressed (internally). You're not aware of this iss

UVa11988-破损的键盘 Broken Keyboard

题目描述 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 (internally). You're not aware of thi

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