HDU 4915 bracket matching + clever Simulation

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4915

Given a sequence, ()? Components, where? It can represent (OR), and there is one, multiple, or no matching.


From left to right, fill n/2 left brackets with priority, and continue to fill the right brackets. If there is a conflict in the process (the number of right parentheses exceeds the number of left parentheses), it is none. Otherwise, you must determine whether the unique solution is a multiple solution.

After the records are exactly filled with n/2 left brackets during the previous traversal, the positions of the right brackets are forcibly set to left parentheses for the first time, and the number of question marks is reduced by one, if the general rule can be run, multiple solutions are provided. Otherwise, a single solution is provided.

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <queue>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define RD(x) scanf("%d",&x)#define RD2(x,y) scanf("%d%d",&x,&y)#define clr0(x) memset(x,0,sizeof(x))typedef long long LL;int n;char s[1000010];int a[1000005],b[1000005],tot,t,m;int main(){    while (scanf("%s",s)!=EOF){        n=strlen(s);        int i,j,k;        tot = m = t = 0;        if(n%2){puts("None");continue;}        for(int i = 0;i < n;++i){            if(s[i] == '('){                a[i] = b[i] = 1;                tot++;            }else if(s[i] == ')')                a[i] = b[i] = -1;            else{                a[i] = b[i] = 0;                m++;            }        }        t = n-tot-m;        if(tot > n/2 || t > n/2){puts("None");continue;}        t = n/2 - tot;        int pos = -1;        for (k = 0,i = 0;i < n;++i){            if (a[i] == 0){                if (t)                    --t,k++;                else{                    if(pos == -1)                        pos = i;                    k--;                }            }            else k+=a[i];            if (k < 0){                puts("None");                goto end;            }        }        if(k != 0){            puts("None");            continue;        }        t = n/2 - tot;        if(t == m || t == 0){            puts("Unique");            continue;        }        --t;        for(b[pos] = 1,k = 0,i = 0;i<n;++i){            if(b[i] == 0){                if (t) --t,k++;                else k--;            }            else k += b[i];            if (k < 0){                puts("Unique");                goto end;            }        }        puts("Many");        end:;    }    return 0;}



HDU 4915 bracket matching + clever Simulation

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.