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键出了问题,会不定时的按下. ...
随机推荐
- Yapi的坑
前一段时间,研究WEB Api相关的工具. YApi 可以内网部署,内心十分的欢喜啊.而且gitHub上推荐超过4000星,成绩很优异嘛.然而通过最终的尝试,我还是打算放弃他,投入Postman的怀抱 ...
- Pacemaker、corosync
pacemaker详细介绍: http://blog.51cto.com/freeloda/1274533 corosync详细介绍: http://blog.51cto.com/freeloda/1 ...
- 椭圆曲线加密和rsa对比
最近在导师的要求下接手了基于欧洲标准的车联网项目中的安全层,需要学习密码学,以及网络安全的相关内容,这里做一个总结 引用的大部分内容为一个西安的大佬(哈哈我老家也是西安的),大佬主页:https:// ...
- mysql中查询常用的关键字
最简单的查询: 1 select * from [where ] 1 select column1,column2....from [where] 这里需要注意的是where子句中条件过滤使用到的关键 ...
- Python读写tap设备
#!/usr/bin/python import os import struct import fcntl import binascii TUNSETIFF = 0x400454ca IFF_TA ...
- TypeScript & Angular
TypeScript https://github.com/Microsoft/TypeScript https://www.typescriptlang.org/ https://www.type ...
- P4712 「生物」能量流动
由于题面$markdown$格式,博主太懒不想一个一个改,所以题面见此:戳 Solution: 本题的贪心思路比较有意思,完全考读题... 首先,因为总的能量来源是$a[0]$,所以可以理解为总能量守 ...
- ss安装教程
https://teddysun.com/342.html 加速 wget –no-check-certificate https://github.com/teddysun/across/raw/m ...
- MAC使用IDA PRO远程调试LINUX程序
1 背景 在学习Linux系统上的一些漏洞知识的时候,往往需要进行“实地测试”,但是在Linux系统上进行调试并不太方便,因为LINUX自带的GDB调试工具真的不太人性化,即使有GDBTUI之类的“伪 ...
- HDU 4667 Building Fence(求凸包的周长)
A - Building Fence Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u ...