<html>

题目链接:hdu 4915 Parenthese sequence

题目大意:给定一个序列。由(,),?组成?能够表示(或者)。问说有一种、多种或者不存在匹配。

解题思路:从左向右,从右向左,分别维护左括号和右括号可能的情况,区间上下界。如果过程中出现矛盾。则为None。否则要推断唯一解还是多解。

枚举每一个问号的位置,如果该问号可为左右括号,则有多解。

#include <cstdio>
#include <cstring>
#include <algorithm>

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;
}
版权声明:本文为博主原创文章,未经博主同意不得转载。 举报
  • 本文已收录于下面专栏:

相关文章推荐

hdu 5014 Number Sequence(贪心)

<a target="_blank" href="http://acm.hdu.edu.cn/showproblem.php

HDU4915 Parenthese sequence

Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O...

hdu 4915 Parenthese sequence(高效)

<a target="_blank" href="http://acm.hdu.ed

HDU 4915 Parenthese sequence

HDU 4915 Parenthese sequence 题目链接 题意:给定一个有?的左右括号串。?

能替代为'('或')',问括号匹配是否唯一或多种或不可能 思路:先从右往左扫一边...

DHOJ1005 HDU1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). <div class="panel_
  • mmdev
  • mmdev
  • 2010-06-25 23:26
  • 393

HDU 4915 Parenthese sequence

#include #include #include using namespace std; char str[1000005]; int maxr[1000005], minr[1000005];...

hdu 4893 Wow! Such Sequence!(线段树)

<a target="_blank" href="http://acm.hdu.ed

hdu 4915 Parenthese sequence

Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O...

hdu 3397 Sequence operation(非常有意思的线段树题)

Sequence operation Time Limit: 10000/5000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4952

hdu 4915 Parenthese sequence

Parenthese sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O...
  • 微博
    微信
    QQ
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多仅仅同意输入30个字)

posted on 2017-08-12 16:44  yjbjingcha  阅读(166)  评论(0编辑  收藏  举报

导航