破损的键盘

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<list>
using namespace std;


int main(void)
{
    string s;
    while (cin >> s)
    {
        list<char> ch;
        list<char>::iterator it = ch.begin();
        for (auto a : s)
        {
            if (a == '[')it = ch.begin();
            else if (a == ']')it = ch.end();
            else
            {
                it =ch.insert(it, a);//insert的这种写法有返回值,需要用迭代器来接受位置否则会发生错误
                it++;
            }
        }
        for (it = ch.begin(); it != ch.end(); it++)
            cout << *it;
        cout << endl;
    }
    return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 100000 + 5;
int last, cur,nexti[maxn];
char s[maxn];
int main(void)
{
    while (scanf("%s", s + 1) == 1)
    {
        int n = strlen(s + 1);
        last = cur = 0;
        nexti[0] = 0;

        for (int i = 1; i <= n; i++)//核心代码
        {
            char ch = s[i];
            if (ch == '[')cur = 0;
            else if (ch == ']')cur = last;
            else {
                nexti[i]  =nexti[cur];
                nexti[cur] = i;
                if (cur == last)last = i;
                cur = i;
            }
        }
        for (int i = nexti[0]; i != 0; i = nexti[i])
            printf("%c", s[i]);
        printf("\n");
    }
    return 0;
}

 

posted @ 2021-02-10 12:57  loliconsk  阅读(109)  评论(0)    收藏  举报