zoukankan      html  css  js  c++  java
  • FZU2030 括号问题(dp)

    Problem 2030 括号问题

    Accept: 398    Submit: 753
    Time Limit: 1000 mSec    Memory Limit : 32768 KB

     Problem Description

    给出一个字符串,其中包括3种字符: ‘(‘, ‘)’, ‘?’.其中?表示这个字符可以是’(‘也可以是’)’. 现在给出字符串S,你可以在’?’处填写’(‘ 或者 ‘)’,当然随意填写得到的序列可能是括号不匹配的。例如”(?”,如果你填写’(‘那么”((“是括号不匹配的! 现在你的任务是确定你有多少种填写方案,使得最终的字符串是括号匹配的!2种方案是不同的,当2种方案中至少存在1个填写字符是不同的。 例如,对于”((??))”,我们可以得到2种方案: “((()))”, “(()())”。

     Input

    数据包含多组测试数据 第一行输入一个字符串S(S的长度不超过16)。

     Output

    输出一个整数,表示合法的填写方案数。

     Sample Input

    ((??))

     Sample Output

    2
     
    思路: dp[i][j]表示第i个位置左右括号抵消后左括号剩下的数量,dp[sz][0]就是答案了
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int INF = 1e9;
    const double eps = 1e-6;
    const int N = 20;
    int cas = 1;
    
    char s[N];
    int dp[N][N];
    
    void run()
    {
        int sz = strlen(s+1);
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=1;i<=sz;i++)
        {
            if(s[i]=='(' || s[i]=='?')
            {
                for(int j=1;j<=sz;j++)
                    dp[i][j]+=dp[i-1][j-1];
            }
            if(s[i]==')' || s[i]=='?')
            {
                for(int j=0;j<=sz;j++)
                    dp[i][j]+=dp[i-1][j+1];
            }
        }
    //    for(int i=1;i<=sz;i++)
    //    {
    //        for(int j=1;j<=sz;j++) cout<<dp[i][j]<<' ';
    //        cout<<endl;
    //    }
        printf("%d
    ",dp[sz][0]);
    }
    
    int main()
    {
        #ifdef LOCAL
        //freopen("case.txt","r",stdin);
        #endif
        while(scanf("%s",s+1)!=EOF)
            run();
        return 0;
    }
  • 相关阅读:
    python协程爬取某网站的老赖数据
    python异步回调顺序?是否加锁?
    go语言循环变量
    使用memory_profiler异常
    安装python性能检测工具line_profiler
    等我!
    pytorch代码跟着写
    Python异常类型总结
    Python项目代码阅读【不断更新】
    夏令营体会
  • 原文地址:https://www.cnblogs.com/someblue/p/4047879.html
Copyright © 2011-2022 走看看