uva11988 - Broken Keyboard (a.k.a. Beiju Text) 模拟 链表

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

  在输入一个串的时候可能会按到home键和end键,[代表home键,]代表end键,输出最终的串。

  设head为整个串的头,tail为尾,找到第一个不是[]的位置,head和tail都初始化为这个位置。flag=1代表按了home键,在前面输入字符,flag=0代表按了end,在后面输入字符。用t记录在前面插入字符时应插入的位置。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include<algorithm>
#include <map>
#include<stack>
#include<queue>
#define MAXN 100010
using namespace std;
char str[MAXN];
int next[MAXN];
int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%s",str)!=EOF){
        int head=-1,tail=-1,flag=0,t;
        memset(next,-1,sizeof(next));
        for(int i=0;i<strlen(str);i++) if(str[i]!='['&&str[i]!=']'){
            head=tail=i;
            break;
        }
        for(int i=head+1;i<strlen(str);i++){
            if(str[i]=='['){
                next[i]=head;
                head=i;
                t=i;
                flag=1;
            }
            else if(str[i]==']'){
                next[tail]=i;
                tail=i;
                flag=0;
            }
            else{
                if(flag){
                    next[i]=next[t];
                    next[t]=i;
                    t=i;
                }
                else{
                    next[tail]=i;
                    tail=i;
                }
            }
        }
        for(int i=head;i!=-1;i=next[i]) if(str[i]!='['&&str[i]!=']') printf("%c",str[i]);
        puts("");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值