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)