tyvj4868 天天和不可描述

描述

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 500010
int len,map[maxn],st[maxn],top,mp[maxn];
char ch[maxn],vis[maxn];
void pr(int l,int r){
    if(l<=r){
        for(int i=l;i<=r;i++){
            if(ch[i]!='('&&ch[i]!=')'){
                printf("%c",ch[i]);continue;
            }
            if(ch[i]=='('){
                if(vis[i]==1)return;
                vis[i]=1;
                pr(map[i]-1,i+1),i=map[i];continue;
            }
            if(ch[i]==')'){
                if(vis[i]==1)return;
                vis[i]=1;
                pr(mp[i]+1,i-1),i=mp[i];continue;
            }
        }
    }
    else {
        for(int i=l;i>=r;i--){
            if(ch[i]!='('&&ch[i]!=')'){
                printf("%c",ch[i]);continue;
            }
            if(ch[i]=='('){
                if(vis[i]==1)return;
                vis[i]=1;
                pr(map[i]-1,i+1),i=map[i];continue;
            }
            if(ch[i]==')'){
                if(vis[i]==1)return;
                vis[i]=1;
                pr(mp[i]+1,i-1),i=mp[i];continue;
            }
        }
    }
}
int main(){
    //freopen("Cola.txt","r",stdin);
    scanf("%s",ch+1);
    len=strlen(ch+1);
    for(int i=1;i<=len;i++){
        if(ch[i]=='(')st[++top]=i;
        if(ch[i]==')')map[st[top]]=i,mp[i]=st[top],top--;
    }
    pr(1,len);
}

 

posted @ 2017-09-22 19:59  Echo宝贝儿  阅读(173)  评论(0编辑  收藏  举报