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

概译:我们在用键盘输入字符串,如果出现' [ '光标就跳到最前面,如果出现' ] '就跳到最后面,给出输入时的字符串输出实际得到的字符串,详见样例。

思路:水题轻喷……模拟,链表插入,可以用数组实现模拟链表。我模拟链表不太熟,第一次是用STL模拟的。可以用一个双端队列deque储存最终结果,遇到一个' [ '就把后面的字符放在stack里,再遇到' [ '或' ] '时把stack里的字符放在deque的front里。PS:可能WA的数据:abc[123[456[ef

STL比较慢,100msAC

 #include <bits/stdc++.h>
using namespace std; int main()
{
string s;
while(getline(cin,s))
{
s+=']';
deque<char>dq;
stack<char>st;
bool state=false; for (int i = ; s[i]; ++i)
{
if (s[i]=='[') state=true;
else if (s[i]==']') state=false; if(state)
{
if(s[i]!='[')
st.push(s[i]);
else
while(!st.empty())
{
dq.push_front(st.top());
st.pop();
}
}
else
{
if(s[i]!=']')
dq.push_back(s[i]);
while(!st.empty())
{
dq.push_front(st.top());
st.pop();
}
}
} while(!dq.empty())
{
printf("%c",dq.front());
dq.pop_front();
}
printf("\n");
}
return ;
}

模拟链表是用一个next数组代替链表中的next指针,比如第一个字符s[1]的下一个是s[2],则next[1]=2。思想上和链表是一样的。另外众所周知,链表的题常常会把第0个作为不储存数据的辅助头结点,第一个下标才开始储存数据。

标程的思路是设置一个光标cur,但这个cur不是当前遍历到的位置i,而代表着位置i的字符应该插入在cur的右侧。期间cur有时会跳至左端即cur=0;有时要回到右端,所以还要开一个last变量保存最右端的下标,使cur=last跳回右端。

代码更精简,30ms,如下:

 #include <bits/stdc++.h>
using namespace std;
#define maxl 100005 int main()
{
char s[maxl];
while(~scanf("%s",s+))
{
int Next[maxl]={};
int cur=,last=; for (int i = ; s[i]; ++i)
{
if(s[i]=='[') cur=;
else if(s[i]==']') cur=last;
else
{
//链表插入操作
Next[i]=Next[cur];
Next[cur]=i;
//last的更新
if(cur==last) last=i;
//cur的更新
cur=i;
}
} for (int i = Next[]; i != ; i = Next[i])
if (s[i]!='['&&s[i]!=']')
printf("%c",s[i]);
printf("\n");
}
return ;
}

UVA11988:悲剧文本(模拟链表)的更多相关文章

  1. 破损的键盘 (Broken Keyboard)--又名悲剧文本(线性表)

     题目: 你有一个破损的键盘.键盘上的所有键都可以正常工作,但有时Home键或者End键会自 动按下.你并不知道键盘存在这一问题,而是专心地打稿子,甚至连显示器都没打开.当你 打开显示器之后, 展现在 ...

  2. COJ 0017 20604悲剧文本

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=17 20604悲剧文本 难度级别:B: 运行时间限制:1000ms: 运行空 ...

  3. Broken Keyboard(悲剧文本)

    你有一个键盘,键盘上所有的键都能正常使用,只是Home键和End键有时会自动按下.你并不知道这一情况,而是专心地打稿子,甚至连显示器都没开电源.当你打开显示器之后,展现在你面前的是一段悲剧文本.你的任 ...

  4. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

  5. hdu5009 Paint Pearls (DP+模拟链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 2014网络赛 西安 比较难的题 Paint Pearls Time Limit: 4000/2000 M ...

  6. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  7. CF 552(div 3) E Two Teams 线段树,模拟链表

    题目链接:http://codeforces.com/contest/1154/problem/E 题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了 分析:看这道题一点思路都没有 ...

  8. C - Boxes in a Line 数组模拟链表

    You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...

  9. 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 wi ...

随机推荐

  1. php-get和post请求

    1.get请求 <?php //判断20130101是否是工作日 //工作日对应结果为 0, 休息日对应结果为 1, 节假日对应的结果为 2: $url='http://www.easybots ...

  2. 如何应用 AutoIt 修改本机的防火墙配置?(开启,关闭防火墙,添加程序信任到防火墙)

    以前,公司的实施人员配置好项目之后,不同的机器之间经常性的无法建立链接,后来发现是防火墙的设置.虽然是个小问题,但是经常性的忘记这个配置. 现在,我决定把对防火墙的设置,加入到我给实施人员的配置工具中 ...

  3. centos7使用ceph-deploy部署ceph

    准备阶段 准备yum源 删除默认的源,国外的比较慢 yum clean all rm -rf /etc/yum.repos.d/*.repo 下载阿里云的base源 wget -O /etc/yum. ...

  4. windows: Python安装scipy,scikit-image时提示"no lapack/blas resources found"的解决方法

    解决方案: 最后,解决我遇到的这个问题的解决方案是来自以下链接的答案: http://www.voidcn.com/blog/z6491679/article/p-5740396.html. 另外还有 ...

  5. JQuery扩展插件Validate—5添加自定义验证方法

    从前面的示例中不难看出validate中自带的验证方法足以满足一般的要求,对于特别的要求可以使用addMethod(name,method,message)添加自定义的验证规则,下面的示例中添加了一个 ...

  6. ASP.NET Web应用程序修改页面Inherits示例

    <@page 中 Codebehind .Inherits 和aspx的关系 CodeBehind 指定包含与页关联的类的已编译文件的名称.该属性不能在运行时使用. 说明: 提供此属性是为了与以 ...

  7. 20个Flutter实例视频教程-第12节: 流式布局 模拟添加照片效果

    视频地址: https://www.bilibili.com/video/av39709290/?p=12 博客地址: https://jspang.com/post/flutterDemo.html ...

  8. 如何将linux主机接入到网络中

    前言: 这篇博客的主旨就想文章题目说的那样,当我们拿到一台新的系统时,我们怎么实现让你的主机连接到网络中.配置网络就是给你的系统配置IP地址,子网掩码,网关以及DNS.也就是说配置好这几项计算机上网的 ...

  9. Redis缓存雪崩、缓存穿透、缓存击穿、缓存降级、缓存预热、缓存更新

    Redis缓存能够有效地加速应用的读写速度,就DB来说,Redis成绩已经很惊人了,且不说memcachedb和Tokyo Cabinet之流,就说原版的memcached,速度似乎也只能达到这个级别 ...

  10. TP5实现签到功能

    基于tp5 模型的一个签到功能: 由于存储所有的签到日期数据库会非常庞大,所以签到日期只存储近三个月的. 具体功能: 1.记录最近一次的签到时间 2.每次签到都会添加15积分 3.有连续签到的记录 C ...