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. Bit与Byte的区别

    在工作中遇到一些概念模糊的地方, 需要记住了bit意为“位”或“比特”,是计算机运算的基础: byte意为“字节”,是计算机文件大小的基本计算单位: 说到usb2.0标准接口传输速率.许多人都将“48 ...

  2. python判断mongodb--find(),find_one()返回是否为空

    conn = MongoClient('127.0.0.1', 27017)db = conn.diffcollection = db['test1']result = collection.find ...

  3. (原、整)BSP的江湖传说

    @author:黑袍小道 查看随缘,当苦无妨,良人可归.     引言 为什么叫江湖传说,因为实现了第一人是卡马克,就这么简单.(不接受那啥) Quake3:http://www.mralligato ...

  4. vue 三目运算

    :class="followed ? 'btn-success':'btn-secondary'"

  5. vue 搜索匹配

    computed: { broSeachData: function() { var browesData = this.browesData, searchVal = this.searchVal; ...

  6. Kd-Tree&Ransac笔记

    关于sift资源总结: http://blog.csdn.net/masibuaa/article/details/9191309 两个比较好的资源: https://my.oschina.net/k ...

  7. PB数据窗口中的几种状态及应用

    数据窗口的状态主要有以下几种: 1)New! 2)NewModified! 3)DataModified! 4)NotModified! 数据窗口可以利用这些状态标志判断数据是否被修改过. 记录和字段 ...

  8. thinkphp3.2 学习笔记 基础篇

    环境要求:PHP5.3以上版本注意:PHP5.3DEV和php6不支持 目录结构 www WEB部署目录(或者子目录)├─index.php 入口文件├─README.md README文件├─App ...

  9. NetScaler的cookieinsert和sourceip联合保持机制

    NetScaler的cookieinsert和sourceip联合保持机制 使用NetScaler的cookieinsert和sourceip联合进行session保持机制即主用cookieinser ...

  10. OAuth2.0 用户验证授权标准 理解

    OAuth2.0是一套标准. 一.问题 这个标准解决了这样的一个问题. 允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用.  ...