【链表】【UVA11988】Broken Keyboard

传送门

明明是道黄题我竟然来写博客……我真的是什么数据结构也不会写了

Description

你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下。你却不知道此问题,而是专心致志地打稿子,甚至显示器都没开。当你打开显示器之后,展现你面前的数一段悲剧文本。你的任务是在显示器打开前计算出这段悲剧的文本。

Input

 给你一段按键的文本,其中'['表示Home键,']'表示End键,输入结束标志是文件结束符(EOF)。

Output

输出一行,即这段悲剧文本。 

Sample Input

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

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

Solution

  这打眼一看就是链表啊……然后我写了一半惊恐地发现我连链表都不会写了……

  用cur记录当前字符放在哪个字符后面,nxt[i]表示下标为i的字符后面的字符的下标。注意加完链表要移动cur。

  链表全程对下标操作!!!别写着写着写迷糊了!!

Code

#include<cstdio>
#include<cstring>
#define maxn 100010

inline void qr(int &x) {
    char ch=getchar();int f=1;
    while(ch>'9'||ch<'0')    {
        if(ch=='-')    f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')    x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x*=f;
    return;
}

inline int max(const int &a,const int &b) {if(a>b) return a;else return b;}
inline int min(const int &a,const int &b) {if(a<b) return a;else return b;}
inline int abs(const int &x) {if(x>0) return x;else return -x;}

inline void swap(int &a,int &b) {
    int c=a;a=b;b=c;return;
}

char word[maxn];
int nxt[maxn];

int main() {
    while(~scanf("%s",word+1)) {
        int l=strlen(word+1);
        int cur=0,now=0;nxt[0]=0;
        for(int i=1;i<=l;++i) {
            char &ch=word[i];
            if(ch=='[')    cur=0;
            else if(ch==']') cur=now;
            else {
                nxt[i]=nxt[cur];nxt[cur]=i;
                if(!(cur^now))    now=i;
                cur=i;
            }
        }
        for(int i=nxt[0];i;i=nxt[i])    putchar(word[i]);
        putchar('\n');
    }
    return 0;
}

Summary

  链表全程对下标操作!!下标!!!!!

   另外写链表的时候如果链子上记录的是它右边是谁就找它左边是谁进行更新……

   我怎么这么水啊emmmmmmm

猜你喜欢

转载自www.cnblogs.com/yifusuyi/p/9205548.html