Fzu2030 brackets (DP)

Source: Internet
Author: User
Problem 2030 parenthesesAccept: 398 submit: 753
Time Limit: 1000 msec memory limit: 32768 kb Problem Description provides a string, which contains three types of characters :'(',')','? '. Where? It indicates that this character can be '(' or ')'. Now the string S is provided. You can go '? 'Fill in '(' or ')'. Of course, the sequence may not match with parentheses. For example "(?", If you fill in '(' Then "(" it is not matching! Now, your task is to determine how many fill schemes you have to fill in, so that the final string matches the brackets! The two schemes are different. When the two schemes have at least one character, they are different. For example, "((??))", We can get two solutions: "()" and "()". The input data contains multiple groups of test data. The first line of the test data is the input string s (the length of S cannot exceed 16 ). Output outputs an integer indicating the number of valid solutions. Sample input ((??)) Sample output2 train of thought: DP [I] [J] indicates the number of left brackets after the left parentheses offset at the I position. DP [SZ] [0] is the answer.
#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\n",dp[sz][0]);}int main(){    #ifdef LOCAL    //freopen("case.txt","r",stdin);    #endif    while(scanf("%s",s+1)!=EOF)        run();    return 0;}

 

Fzu2030 brackets (DP)

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.