Hdu 4915 Parenthese sequence (efficient)

Source: Internet
Author: User

Hdu 4915 Parenthese sequence (efficient)

Link: hdu 4915 Parenthese sequence

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

Solution: Maintain the possible conditions of left and right brackets from left to right and the upper and lower bounds of the range respectively. If there is a conflict in the process, it is None. Otherwise, you must determine whether the unique solution is a multiple solution. Enumerate the positions of each question mark. If the question mark can be left or right parentheses, multiple solutions are provided.

#include 
  
   #include 
   
    #include using namespace std;const int maxn = 1e6+5;char s[maxn];int n, l[maxn][2], r[maxn][2];bool check (int x) {    int ldown = l[x-1][0] + 1;    int lup = l[x-1][1] + 1;    if (ldown > r[x+1][1] || lup < r[x+1][0])        return false;    int rdown = r[x+1][0] + 1;    int rup = r[x+1][1] + 1;    if (rdown > l[x-1][1] || rup < l[x-1][0])        return false;    return true;}int judge () {    n = strlen(s+1);    if (n&1)        return 0;    memset(l[0], 0, sizeof(l[0]));    memset(r[n+1], 0, sizeof(r[n+1]));    for (int i = 1; i <= n; i++) {        if (s[i] == '(') {            l[i][0] = l[i-1][0] + 1;            l[i][1] = l[i-1][1] + 1;        } else if (s[i] == ')') {            if (l[i-1][1] == 0)                return 0;            l[i][0] = (l[i-1][0] == 0 ? l[i-1][0] + 2 : l[i-1][0]) - 1;            l[i][1] = l[i-1][1] - 1;        } else {            l[i][0] = (l[i-1][0] == 0 ? l[i-1][0] + 2 : l[i-1][0]) - 1;            l[i][1] = l[i-1][1] + 1;        }    }    for (int i = n; i; i--) {        if (s[i] == ')') {            r[i][0] = r[i+1][0] + 1;            r[i][1] = r[i+1][1] + 1;        } else if (s[i] == '(') {            if (r[i+1][1] == 0)                return 0;            r[i][0] = (r[i+1][0] == 0 ? r[i+1][0] + 2 : r[i+1][0]) - 1;            r[i][1] = r[i+1][1] - 1;        } else {            r[i][0] = (r[i+1][0] == 0 ? r[i+1][0] + 2 : r[i+1][0]) - 1;            r[i][1] = r[i+1][1] + 1;        }    }    for (int i = 1; i <= n; i++)        if (s[i] == '?' && check(i))            return 2;    return 1;}int main () {    while (scanf("%s", s+1) == 1) {        int flag = judge();        if (flag == 2)            printf("Many\n");        else if (flag == 1)            printf("Unique\n");        else            printf("None\n");    }    return 0;}
   
  

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.