https://vjudge.net/problem/UVA-11988
#include<cstdio>
#include<math.h>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
#define rep(i,j,k)for(i=j;i<k;i++)
#define per(i,j,k)for(i=j;i>k;i--)
#define MS(x,y)memset(x,y,sizeof(x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define abs(x) (x>0?x:-x)
const int INF=0x7ffffff;
const ll MAX=1e18;
string str;
list<char>s;
list<char>::iterator it=s.begin();
int main()
{
while(cin>>str){
for(int i=0;i<str.length();i++){
if(str[i]=='[')it=s.begin();
else if(str[i]==']')it=s.end();
else s.insert(it,str[i]);
}
for(it=s.begin();it!=s.end();it++)
printf("%c",*it);
printf("\n");
s.clear();
}
return 0;
}
#include<cstdio>
#include<math.h>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<climits>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
#define rep(i,j,k)for(i=j;i<k;i++)
#define per(i,j,k)for(i=j;i>k;i--)
#define MS(x,y)memset(x,y,sizeof(x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define abs(x) (x>0?x:-x)
const int INF=0x7ffffff;
const ll MAX=1e18;
const int M=1e5+10;
int last,cur,nex[M];
char s[M];
int main()
{
while(scanf("%s",s+1)==1){
int len=strlen(s+1);
last=cur=0;
nex[0]=0;
for(int i=1;i<=len;i++){
if(s[i]=='[')cur=0;
else if(s[i]==']')cur=last;
else{
nex[i]=nex[cur];
nex[cur]=i;
if(cur==last)last=i;
cur=i;
}
}
for(int i=nex[0];i!=0;i=nex[i])
printf("%c",s[i]);
printf("\n");
}
return 0;
}