Broken Keyboard (a.k.a. Beiju Text) UVA - 11988(链表)

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

链表

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 100005;

int nex[maxn],cur,tot,pre;
char to[maxn];
char s[maxn];

void add(int x)
{
    nex[++tot] = nex[cur];
    nex[cur] = tot;
    to[tot] = s[x];
    if(pre == cur)pre = tot;
    cur = tot;
}

int main()
{
    while(~scanf("%s",s + 1))
    {
        memset(nex,0,sizeof(nex));
        int len = (int)strlen(s + 1);
        tot = 0;
        cur = 0;pre = 0;
        for(int i = 1;i <= len;i++)
        {
            if(s[i] != '[' && s[i] != ']')
            {
                add(i);
            }
            else if(s[i] == '[')
            {
                cur = 0;
            }
            else if(s[i] == ']')
            {
                cur = pre;
            }
        }
        
        for(int i = nex[0];i;i = nex[i])
        {
            printf("%c",to[i]);
        }
        printf("\n");
    }
    return 0;
}

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int nex[100005];
char s[100005];

int main()
{
    while(~scanf("%s",s + 1))
    {
        int pre = 0,cur = 0;
        int len = strlen(s + 1);
        nex[0] = 0;
        
        for(int i = 1;i <= len;i++)
        {
            if(s[i] == '[')
            {
                cur = 0;
            }
            else if(s[i] == ']')
            {
                cur = pre;
            }
            else
            {
                nex[i] = nex[cur];
                nex[cur] = i;
                if(cur == pre)pre = i;
                cur = i;
            }
        }
        
        for(int i = nex[0];i;i = nex[i])
        {
            printf("%c",s[i]);
        }
        printf("\n");
    }
    return 0;
}

发布了756 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104435537