洛谷 P3015 [USACO11FEB]最好的括号Best Parenthesis

传送门

题目大意:给出括号的得分标准。

()得分为1,如果A的得分为S(A),那么

(A)的得分为2*S(A)。

题解:搜索

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 100009
#define LL long long
#define mod 12345678910LL
using namespace std;

int n,top;

int sta[N],pos[N];

LL dfs(int l,int r){
    LL ret=0;
    int rr=pos[l];
    if(rr-l==1)ret=(ret%mod+1%mod)%mod;
    if(l!=rr-1)ret=(ret%mod+2*dfs(l+1,rr-1)%mod)%mod;
    if(rr+1<=r)ret=(ret%mod+dfs(rr+1,r)%mod)%mod;
    return ret%mod;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        if(x==0)
         sta[++top]=i;
        else 
         if(top)
          pos[sta[top--]]=i;
    }
    cout<<dfs(1,n);
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zzyh/p/7788260.html