首页 > 代码库 > Broken Keyboard (a.k.a. Beiju Text)

Broken Keyboard (a.k.a. Beiju Text)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<algorithm>
#include<functional>
#include<sstream>
using namespace std;
#define N 100005
char a[N];
int nest[N];
char ans[N];
int main()
{
    int i, j, len;
    while (cin >> a)
    {
        int k, cur, last;
        k = cur = last = 0;
        nest[0] = -1;
        for (i = 0; a[i]; i++)
        {
            if (a[i] == [)
                cur = 0;
            else
                if (a[i] == ])
                    cur = last;
                else
                {
                    k++;
                    ans[k] = a[i];
                    nest[k] = nest[cur];
                    nest[cur] = k;
                    cur = k;
                    if (nest[k] == -1) last = k;
                }
        }
        int t = nest[0];
        while (t != -1)
        {
            printf("%c", ans[t]);
            t = nest[t];
        }
        printf("\n");
    }
    return 0;
}

 

数组模拟链表

技术分享

 

Broken Keyboard (a.k.a. Beiju Text)