Fzu 2030 parentheses problem

Source: Internet
Author: User

Two practices, a DP, a DFS, because this data is relatively small, so the DFS is acceptable in the full array of ways, but when the big time is not, so DP way to grasp, I here is the practice of DFS, online there are many people write DP, you can go to see, Especially when traversing to the right parenthesis, you need to think about it.

#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Chara[ -];intlen,ans,mark[ -];BOOLok () {intLena = strlen (a), tot =0;  for(inti =0; I < lena;i++)    {        if(A[i] = ='(') Tot++; if(A[i] = =')') Tot--; if(A[i] = ='?')            return false; if(Tot <0)return false; }    if(Tot! =0)return false; return true;}voidDfsintNow ) {    if(now = =Len) {        if(OK ()) ans++; return; } A[mark[now]]='('; DFS ( now+1); A[mark[now]]=')'; DFS ( now+1);}intMain () { while(~SCANF ("%s", a)) {        intLena =strlen (a); Len=0;  for(inti =0; I < lena;i++)        {            if(A[i] = ='?') {Mark[len++] =i; }} ans=0; DFS (0); printf ("%d\n", ans); }    return 0;}

Fzu 2030 parentheses problem

Related Article

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.