P4868 天天和不可描述

http://www.tyvj.cn/p/4868

思路:

  本想用站做的,但发现要用很多站同时做,还要来回倒。

  我怕超时,所以换了种做法。

  因为每遇到一次括号都要把输出方向改变,而括号是成对存在的,所以不用担心会走错(放心模拟就行)。

  还有就是,遇到括号是要进行转跳的,所以预处理出,对应括号的位置即可。

  然后模拟着走就ok。

#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
#define N 500009
char s[N];
int len;
int cnt,l[N],last[N],x,dir;
int main()
{
    cin>>(s+1);
    len=strlen(s+1);
    for(int i=1;i<=len;i++)
    {
        if(s[i]=='(')
            l[++cnt]=i;
        else
        if(s[i]==')')
        {
            last[i]=l[cnt];//
            last[l[cnt--]]=i; //
        }
    }
    dir=1;x=1;
    while(x>=1&&x<=len)
    {
        if(last[x])
        {
            x=last[x];
            dir*=-1;
        }
        else
            printf("%c",s[x]);
        x+=dir;
    }
    return 0;
} 
原文地址:https://www.cnblogs.com/CLGYPYJ/p/7616999.html