UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) (简单链表)

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]  




题意:

对于输入的字符串,[ ]是光标控制符,[表示光标移动到最前,]光标移动到最后。问最后的字符串是什么。


数组简单模拟一下链表就行了 对于插入,判断一下是不是在最后,如果是,end指针后移


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 6000000 + 20;
char str[maxn];
int next[maxn];

int main() {

    while(gets(str+1) != NULL) {
        int n = strlen(str+1);
        int head = 0;
        memset(next, -1, sizeof(next));
        int i = 1;
        int p = head;
        int end = head;
        int isend = 1;
        int sz = 0;
        while(i <= n) {
            if(str[i] == '[') {
                p = head;
                if(sz > 0) isend = 0;
            } else if(str[i] == ']'){
                p = end;
                isend = 1;
            } else {
                next[i] = next[p];
                next[p] = i;
                p = i;
                sz++;
                if(isend) end = i;
            }
            i++;
        }
        i = next[head];
        while(i != -1) {
            putchar(str[i]);
            i = next[i];
        }
        putchar('\n');
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值