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");
    }
    ;
}
05-04 11:51