原题及翻译

Broken Keyboard (a.k.a. Beiju Text)
破碎的键盘(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 (internally).
键盘的唯一问题是有时“自动”键或“结束”键被自动按下(内部)。
You’re not aware of this issue, since you’re focusing on the text and did not even turn on the monitor!
你不知道这个问题,因为你专注于文本,甚至没有打开监控!
After you finished typing, you can see a text on the screen (if you turn on the monitor).
键入完成后,您可以在屏幕上看到文本(如果打开显示器)。
In Chinese, we can call it Beiju.
在中文里,我们可以称之为悲剧。
Your task is to find the Beiju text.
你的任务是找到悲剧文本。

Input

There are several test cases.
有几个测试用例。
Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’.
每个测试用例都是一行,包含至少一个,最多100,000个字符,下划线和两个特殊字符’[‘和’]’。
‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally.
'[‘表示内部按下“Home”键,’]表示内部按下“End”键。
The input is terminated by end-of-file(EOF).
输入由文件结束(EOF)终止。

Output

For each case, print the Beiju text on the screen.
对于每种情况,请在屏幕上打印悲剧文本。

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

分析

最简单的想法便是使用数组来保存这段文本,然后用一个变量post保存“光标位置”。这样,输入一个字符相当于在数组中插入一个字符(需要先把后面的字符全部右移,给新字符腾出位置)。

但是,这样的代码会超时,因为输入一个字符都可能会引起大量字符移动。

解决方案是采用链表(linked list)。每输入一个字符就把它存起来,设输入字符串是s[1~n],则可以用next[i]表示在当前显示屏中s[i]右边的字符编号(即在s中的下标)。

在数组中频繁移动元素是很低效的,如有可能,可以使用链表。

为了方便起见,假设字符串s的最前面还有一个虚拟的s[0],则next[0]就可以表示显示屏最左边的字符。再用一个变量char表示光标位置:即当前光标位于s[cur]的右边。cur=0说明光标位于“虚拟字符”s[0]的右边,即显示屏的最左边。

为了方便起见,常常在链表的第一个元素之前放一个虚拟结点。

为了移动光标,还需要一个变量last表示显示屏的最后一个字符是s[last]。

代码

#include <cstdio>
#include <cstring>
const int maxn=100000+5;
int last,cur,next[maxn];    //光标位于cur号字符的后面
char s[maxn];int main()
{while(scanf("%s",s+1)==1){int n=strlen(s+1);    //输入保存在s[1],s[2]…中last=cur=0;next[0]=0;for(int i=1;i<=n;i++){char ch=s[i];if(ch=='[') cur=0;else if(ch==']') cur=last;else{next[i]=next[cur];next[cur]=i;if(cur==last) last=i;    //更新“最后一个字符”编号cur=i;    //移动光标}}for(int i=next[0];i!=0;i=next[i])printf("%c",s[i]);printf("\n");}return 0;
}

Broken Keyboard (a.k.a. Beiju Text)相关推荐

  1. 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 ...

  2. UVA11988 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 problemwit ...

  3. UVA 11988——Broken Keyboard (a.k.a. Beiju Text)

    题意:给定一个字符串,然后[会将光标跳转到头,]会将光标调到尾,问最后正确的输入. 思路:直接用list来模拟即可,[的时候就在头插,]就在尾插,也可根据递归顺序解. code: #include & ...

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

    题目链接:https://vjudge.net/problem/UVA-11988 题目大意:输入一个字符串,输出在原本应该是怎么样的?  具体方法是 碰到' [ ' 回到最前面  碰到' ]'  回 ...

  5. UVa11988 Broken Keyboard (a.k.a. Beiju Text)

    题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...

  6. 例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988)

    例题6-4 破损的键盘(又名:悲剧文本)(Broken Keyboard(a.k.a. Beiju Text), UVa 11988) 链表的应用 #include<iostream> # ...

  7. 【刘汝佳代码详解】例题6-4破损的键盘(Broken Keyboard,UVa 11988)

    立志用最少的代码做最高效的表达 You're typing a long text with a broken keyboard. Well it's not so badly broken. The ...

  8. UVa11988-破损的键盘 Broken Keyboard

    题目描述 You're typing a long text with a broken keyboard. Well it's not so badly broken. The only probl ...

  9. PAT Broken Keyboard (20)

    题目描写叙述 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the cha ...

最新文章

  1. 在用户控件中弹出消息框的方法
  2. codeblock生成64位dll_Pythonnet/clr : Unable to find assembly xxxx.dll
  3. tf.clip_by_value
  4. 如何积累自己的技术认知
  5. was java heap space_实战项目中Java heap space错误的解决
  6. markdown单元格快速合并(不用自己写html代码)
  7. 电气控制原理动图22张,超赞!
  8. scatter函数_matplotlib.pyplot常用函数scatter讲解大全(三)
  9. 那些文献阅读能力爆表的科研学子,都在偷偷做这件事……
  10. 怎么更好掌握Web前端技术?JS的跨域是怎么回事?
  11. jQuery Mobile中的页面加载与跳转机制
  12. tensorflow随笔——concat(), stack(), unstack()
  13. 10+年程序员告诉你职场误区,如何快速提升自己?
  14. 触动精灵手游脚本开发框架FreeGame源码
  15. vue在开发环境中配置本地hosts修改域名
  16. wmic卸载程序命令_使用WMIC命令行卸载软件
  17. 基于Proteus学习单片机系列(五)——定时器实现电子表
  18. 闵华 oracle,Oracle 数据库上机试验指导1(答案).doc
  19. matlab三维点云去除背景,一种点云处理三维重建方法与流程
  20. spring boot 源码解析23-actuate使用及EndPoint解析

热门文章

  1. AtCoder - 2153 An Ordinary Game list模拟 || 博弈
  2. JS DOM 对象
  3. 静态库与动态库(一):概述
  4. 在路上(on the road)
  5. Crystal Reports basic for Visual Studio 2008部署
  6. python测试需要学什么_从手工测试到自动化测试需要学什么?
  7. 实验7.3 字符串 7-7 输出大写英文字母
  8. thymeleaf 中文_springboot 整合 thymeleaf(上手即用)
  9. php实现电话拨打,jquery mobile实现拨打电话功能的几种方法_jquery
  10. Install stardict on Ubuntu