s[] 数组用来保存原有的字符序列
nex[] 数组记录打印的下标顺序
C++11 代码如下:
#include<iostream>
#include<cstring>
using namespace std;
#define maxn 100006
char s[maxn];
int nex[maxn], n; int main() {
while (cin >> s + ) {
int cur = , last = ;
nex[] = ;
char ch;
for (int i = ; i <=strlen(s + ); i++) {
ch = s[i];
if (ch == '[') cur = ;
else if (ch == ']') cur = last;
else {
nex[i] = nex[cur];
nex[cur] = i;
if (cur == last)last = i;
cur = i;
}
}
for (int i = nex[]; i != ; i = nex[i])
cout << s[i];
cout << endl;
}
return ;
}