Fzu 2030 parentheses problem (DFS)

Source: Internet
Author: User

problem 2030 parentheses problemaccept:413 submit:804
Time limit:1000 mSec Memory limit:32768 KB problem description gives a string that consists of 3 characters: ' (', ') ', '? '. Which means that the character can be ' (' or ' can be ') '. Now give the string s, which you can do in '? ' Fill in the ' (' or ') ', of course, the randomly filled sequence may be mismatched brackets. For example "(?" If you fill in ' (' Then ') ("is the parentheses do not match! Now your task is to determine how many kinds of filling schemes you have, so that the final string is matched by parentheses! 2 scenarios are different when there are at least 1 fill characters in 2 scenarios. For example, for "((?))", we can get 2 kinds of Scenarios: "(())", "(() ())", "(() ())". Input data contains multiple sets of test data the first line enters a string s (s length of not more than 16). Output outputs an integer that represents the number of valid completion schemes. Sample Input ((??)) Sample Output2


Ideas:
Code:
#include <cstdio> #include <cstring>using namespace Std;char s[20];int len;int ans;void DFS (int i,int cnt)// CNT stands for mismatched (the number of {    if (i==len&&cnt==0)    {        ans++;    }    if (cnt<0| | I>=len)        return;    if (s[i]== ' (')        DFS (i+1,cnt+1);    else if (s[i]== ') ')        DFS (i+1,cnt-1);    else    {        DFS (i+1,cnt+1);        DFS (i+1,cnt-1);}    } int main () {    while (scanf ("%s", s) ==1)    {        Len=strlen (s);        ans=0;        DFS (0,0);        printf ("%d\n", ans);    }    return 0;}


Fzu 2030 parentheses problem (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.