#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(" "); } return 0; }
数组模拟链表