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)的更多相关文章

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

    题目代号:UVA 11988 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&pa ...

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

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

    题目传送门 题意:训练指南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const ...

  4. UVa 11988 Broken Keyboard (a.k.a. Beiju Text)

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

  5. 暑假集训单切赛第二场 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)(字符串处理)

    一开始不懂啊,什么Home键,什么End键,还以为相当于括号,[]里的东西先打印出来呢.后来果断百度了一下. 悲催啊... 题意:给定一个字符串,内部含有'['和']'光标转移指令,'['代表光标移向 ...

  6. UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

    刘汝佳的题目,悲剧文本 -_-||| 这里使用vector<string>容器倒置记录数据,然后从后面输出就能够了. 难度就是不知道这种文档究竟哪里是開始输出,故此使用动态管理内存的容器比 ...

  7. UVA 11988 Broken Keyboard (a.k.a. Beiju Text) (链表,模拟)

    使用list来模拟就行了,如果熟悉list,那么这道题真是分分钟秒掉... list是双向循环链表,插入和删除操作非常快,缺点是不能像数组一样随机按下标读取. 一下是wiki上说明的相关函数:http ...

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

  9. 破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988)

    破损的键盘(悲剧文本)(Broken Keyboard(a.k.a. Beiju Text),Uva 11988) 题意描述 你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下. ...

随机推荐

  1. 【Matrix Factorization】林轩田机器学习技法

    在NNet这个系列中讲了Matrix Factorization感觉上怪怪的,但是听完第一小节课程就明白了. 林首先介绍了机器学习里面比较困难的一种问题:categorical features 这种 ...

  2. leetcode 【 Remove Duplicates from Sorted List 】 python 实现

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  3. 【转载】Unity3D研究院之与根据动态的两个轨迹点绘制面详解

    大家应该知道3D世界中任何的面都是由三角形绘制完成的,因为任何无规则的集合图形都可以由三角形来组成.比如四边形,无论是正四边形还是无规则四边形都可以由两个三角形拼接而成.结合本文的标题大家仔细想想,如 ...

  4. 求:斐波那契数列的第n项

    def he (n): if n < 3 : return 1 return he(n-1)+he(n-2)print(he(n))

  5. 爬虫:Scrapy16 - Spider Contracts

    Scrapy 通过合同(contract)的方式来提供了测试 spider 的集成方法. 可以硬编码(hardcode)一个样例(sample)url,设置多个条件来测试回调函数处理 response ...

  6. ZOJ 3606 Lazy Salesgirl ( 线段树 + 思路 )

    卖切糕的小女孩 http://www.cnblogs.com/wuyiqi/archive/2012/04/28/2474672.html #include <cstdio> #inclu ...

  7. HDU 3856 Palindrome ( Manacher + RMQ + 二分 ) WA!!!

    不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include < ...

  8. iis如何处理并发请求

    文章:IIS是怎么处理同时到来的多个请求的? 文章:你真的了解:IIS连接数.IIS并发连接数.IIS最大并发工作线程数.应用程序池的队列长度.应用程序池的... 文章:IIS最大工作进程数设置引发串 ...

  9. mysql备份策略

    1.备份的种类 完全备份,就是备份整个数据库对象 事务日志备份, 备份事务日志记录上一次的数据库改变 增量备份,也叫差异备份 文件备份 2.备份方式 逻辑备份, 既备份sql语句,使用mysql自带的 ...

  10. spring security注解(1)

    Chapter 15. 基于表达式的权限控制 Spring Security 3.0介绍了使用Spring EL表达式的能力,作为一种验证机制 添加简单的配置属性的使用和访问决策投票,就像以前一样. ...