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)