2017-8-21破损的键盘 UVa 11988

>


UVA - 11988
Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

Problem 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 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). The size of input file does not exceed 5MB.

Output

For each case, print the Beiju text on the screen.

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Output for the Sample Input

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

Rujia Liu’s Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!

Source

Root :: Prominent Problemsetters ::  Rujia Liu
Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries ::  C++ STL list (Java LinkedList)
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 6. Data Structures ::  Examples

Root :: Rujia Liu’s Presents ::  Present 3: A Data Structure Contest
Root :: AOAPC I: Beginning Algorithm Contests – Training Guide (Rujia Liu) :: Chapter 3. Data Structures :: Fundamental Data Structures ::  Exercises: Beginner
Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Data Structures and Libraries :: Linear Data Structures with Built-in Libraries ::  Basic Data Structures

[]   [Go Back]   [Status]  

一直对利用简单静态链表解决问题不甚了解,趁着这个问题的同时也熟悉一下静态链表的知识和思想:

首先拿到这道题肯定是要对过程进行模拟,再来看看题:每次输入都是在当前指针位置之后进行输入,而我们要进行模拟的就是定位光标位置,[ 即移动到最前端,] 移动到最后端
首先我想到的是vector= =!是不是很扯淡的玩意,忘记了这玩意的挪动跟数组是一个样的。。。TML了一次
再后来自然而然的想到过deque但是同样还是不合适(如果非得用这玩意的话,还得加一个stack之类的作为前置顺序置换),还不如list直接放置来的痛快。。。
= =说了那么多废话,还不是忘了链表这玩意? 接下来就是正主了:list

#include <iostream>
#include <string>
#include <list>
using namespace std;

string line;
list<char> out;

int main(){
    while(getline(cin,line)){
        int len = line.length();
        list<char>::iterator iter;
        iter = out.begin();
        for(int i = 0; i < len; i ++){
            if(line[i] == '[')
                iter = out.begin();
            else if(line[i] == ']')
                iter = out.end();
            else
                out.insert(iter,line[i]);
        }
        for(iter = out.begin(); iter != out.end(); iter ++)
            cout << *iter;
        cout << endl;

    }
    return 0;
}

这样写其实是可以的,但是针对这道题来说明显是浪费的(指针也要占空间),所以最佳的解决方案应该是利用数组静态链表:(这个代码是算法竞赛经典入门(第二版)书中给出的标程)

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100000 + 5;
int next[maxn];
char line[maxn];

int main(){
    while(scanf("%s",line+1)){  //这里使用line+1的原因纯粹是因为下标方便
        memset(next,0,sizeof(next));
        int len = strlen(line+1);
        int cur = 0;//当前光标位置
        int last = 0;//末尾位置,方便遇到]的时候直接进行定位
        for(int i = 1; i <= len; i ++){

            if(line[i] == '[')
                cur = 0;
            else if(line[i] == ']')
                cur = last;
            else{
                next[i] = next[cur];//每个字符都应该连接到前一个字符(也就是当前光标位置)之后  //这一步相当于外部块链接
                next[cur] = i;  //这一步相当于内部关系链接
                if(cur == last) last = i;
                cur = i;//移动光标
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值