zoukankan      html  css  js  c++  java
  • FZU2030 括号问题[爆搜||DP]

    Problem 2030 括号问题

    Accept: 217    Submit: 417
    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

    Source

    福州大学第八届程序设计竞赛
     
     
     
     
     
     
     
     
    code:
     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 char str[1010];
     6 int len;
     7 int cnt;
     8 
     9 void fun(int pos,int k)
    10 {
    11     if(pos==len&&k==0)
    12     {
    13         cnt++;
    14     }
    15     else if(k<0)
    16     {
    17     }
    18     else if(str[pos]=='?')
    19     {
    20         fun(pos+1,k-1);
    21         fun(pos+1,k+1);
    22     }
    23     else if(str[pos]=='(')
    24     {
    25         fun(pos+1,k+1);
    26     }
    27     else if(str[pos]==')')
    28     {
    29         fun(pos+1,k-1);
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     while(gets(str))
    36     {
    37         cnt=0;
    38         len=strlen(str);
    39         fun(0,0);
    40         printf("%d\n",cnt);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    第八章 多线程编程
    Linked List Cycle II
    Swap Nodes in Pairs
    Container With Most Water
    Best Time to Buy and Sell Stock III
    Best Time to Buy and Sell Stock II
    Linked List Cycle
    4Sum
    3Sum
    Integer to Roman
  • 原文地址:https://www.cnblogs.com/XBWer/p/2689922.html
Copyright © 2011-2022 走看看