UVA——11988 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 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 ‘]’. ‘[’ means the “Home” key is pressed internally,
and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file (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
看到这个题我们是不是就想到我们可以用一个数组储存一下每一个字母然后当这个字母要换到前面的时候,我们再讲这些字母跟后面的字母换一下位置不就好了吗,但是我们考虑一下时间复杂度,我们要将前面的字母挨个移到后面,然后在for循环将后面的字母都到前面,这样的话如果我们遇到的是a与[交替出现的字符串,如果这个字符串长度达到25000那么我们的时间复杂度就已经达到了6*10^9了,哈哈,是不是就T成狗了,所以我们要想个好点的做法,我们把知道链表可以做到数组的插入,因此我们考虑用链表来做这道题
讲解详见代码
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define N 101000 using namespace std; char s[N]; int now,last,net[N]; //我们用now记录当前字母的前驱,last记录我们更正顺序以后的新串中的最后一个字母在原串中的位置,net[i]记录i后面的字母的位置 int main() { )==)//读入字符串 { );//计算字符串的长度 now=last=,net[]=;//初始化 ;i<=l;i++) { ;//我们遇到[我们需要讲这个[]里面的字母全部移动到最前面,因此括号里面第一个字母的前驱为0 else if(s[i]==']') now=last;//我们已经处理完[]里面的字母了,接下来我们要接着处理,我们[]出来以后的第一个字母的前驱为新串中的最后一个字母在原串 中的位置 //例如This_is_a_[Beiju]_text我们处理完[Beiju]以后接下来扫到的字母为_因为我们已经把[Beiju]放到最前面了,那么_的前驱即为_,因此now=last else // { net[i]=net[now];//个人认为他除了在最后记录一下链表结束的情况没有什么卵用,因为nex[i]在后面还会被赋值 net[now]=i;//记录now的后记 if(now==last) last=i;//当now==last的时候只有两种情况,一种是还没有开始处理[]一种是[]已经被处理完了,在这种情况下我们需要更新last值,否则因为我们要把[]移到最前面,那么新串中的最后一个字母一定不是括号中的这些字母 now=i; } } ];i!=;i=net[i]) printf("%c",s[i]); printf("\n"); } ; }
UVA——11988 Broken Keyboard (a.k.a. Beiju Text)的更多相关文章
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(链表)
题目代号:UVA 11988 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&pa ...
- UVa 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 problem wi ...
- 链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)
题目传送门 题意:训练指南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const ...
- UVa 11988 Broken Keyboard (a.k.a. Beiju Text)
题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...
- 暑假集训单切赛第二场 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(字符串处理)
一开始不懂啊,什么Home键,什么End键,还以为相当于括号,[]里的东西先打印出来呢.后来果断百度了一下. 悲催啊... 题意:给定一个字符串,内部含有'['和']'光标转移指令,'['代表光标移向 ...
- UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解
刘汝佳的题目,悲剧文本 -_-||| 这里使用vector<string>容器倒置记录数据,然后从后面输出就能够了. 难度就是不知道这种文档究竟哪里是開始输出,故此使用动态管理内存的容器比 ...
- UVA 11988 Broken Keyboard (a.k.a. Beiju Text) (链表,模拟)
使用list来模拟就行了,如果熟悉list,那么这道题真是分分钟秒掉... list是双向循环链表,插入和删除操作非常快,缺点是不能像数组一样随机按下标读取. 一下是wiki上说明的相关函数:http ...
- 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 i ...
- 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)
破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...
随机推荐
- 24、php知识点总结基础教程--part-2
1.表单处理 ①post请求 <html> <body> <form action="welcome.php" method="post&q ...
- Percona-Tookit工具包之pt-duplicate-key-checker
Preface I suppose that we have a requirement of checking out how many duplicated indexes on ...
- web自动化测试,定位不到元素的原因及解决方案(持续更新中2018年9月29日)
主要讲自己在实战中遇到的坑: 1.动态id定位不到元素 分析原因:每次打开页面,ID都会变化.用ID去找元素,每次刷新页面ID都会发生变化. 解决方案:推荐使用xpath的相对路径方法或者cssSel ...
- save?commit
数据库的隐式提交 先看一段SQL,最后一条SQL的输出你认为是什么? 1 2 3 4 5 6 7 SET AUTOCOMMIT = 1; BEGIN; INSERT INTO t1 VALUES (1 ...
- NGUI-为Popuplist的下拉选项添加删除功能
NGUI例子里的popuplist是这样的:,但有时我们希望下拉选项都有删除功能,也就是这样:,一种方法是改popuplist的源码,我想这个实现起来不难,但现在我想说的是用反射来实现此功能,以及其他 ...
- Could not automatically select an Xcode project. Specify one in your Podfile like so
需要将Podfile文件放置在根目录下,而不能放置在项目的里面. 更改路径即可
- java GUI Graphics2D 绘图
Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...
- [CF1036C]Classy Numbers
题目大意:多个询问,每个询问问$[l,r](1\leqslant l\leqslant r\leqslant10^{18})$内有多少个数满足非零数位小于等于$3$. 题解:数位$DP$,$f_{i, ...
- BZOJ2957 楼房重建 【线段树】
题目 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子. 为了简化问题,我们考虑这些事件发生在一个二维 ...
- 快速配置vim+ctags+taglist
实验平台:centos 7 1.安装vim编辑器 点击(此处)折叠或打开 $sudo yum install vim 并编辑配置文件.vimrc文件,放在主目录下/home/developer 参考配 ...