比赛 近5年noip/csp题目回顾 评测结果 WWWTTWTTWTTTTTTTTTTT
题目名称 括号序列 最终得分 0
用户昵称 ┭┮﹏┭┮ 运行时间 15.016 s
代码语言 C++ 内存使用 4.59 MiB
提交时间 2022-06-27 11:01:00
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000000007;
long long s;
int n,m,k,top;
char a[510],t[510];
bool py(){
    top = 0;
    if(a[1] == '*' || a[n] == '*')return 0;
    for(int i = 1;i <= n;i++){
        if(a[i] == '(' || a[i] == '*'){
            top++;
            t[top] = a[i];
        }
        else if(a[i] == ')'){
            while(t[top] != '('){
                if(top == 0)return 0;
                top--;
            }
            top--;
            if(t[top] == '*' && a[i+1] == '*')return 0;
        }
    }
    while(t[top] == '*')top--;
    if(top == 0){
      return 1; 
    }
    return 0;
}
void sou(int x,int y){
    int i = x;
    if(y > m)return;
    if(x == n+1 || (x == n && a[n] != '?')){
        if(py()){
            s += 1;
            s %= N;
        }
        return;
    }
    if(a[x] != '?'){
        for(i = x;a[i] != '?';i++);
    }
    a[i] = '*';
    sou(i+1,y+1);
    a[i] = '?';
    a[i] = '(';
    sou(i+1,y);
    a[i] = '?';
    a[i] = ')';
    sou(i+1,y);
    a[i] = '?';
}
int main(){
    freopen("2021bracket.in","r",stdin);
    freopen("2021bracket.out","w",stdout);
    cin>>n>>m;
    scanf("%s",a+1);
    for(int i = 1;i <= n;i++){
        if(a[i] == '*')k++;
    }
    sou(1,k);
    cout<<s<<endl;
    
    return 0;
}