首页 > 代码库 > HDU 4915 Parenthese sequence _(:зゝ∠)_ 呵呵

HDU 4915 Parenthese sequence _(:зゝ∠)_ 呵呵

呵呵不是我做的

#include <cstdio>
#include <cstring>
#include <algorithm>
const int N = 1000000 + 10;
char s[N];
int d[N], num[N];

int main() {
    while (~scanf("%s", s)) {
        memset(num, 0, sizeof num);
        int len = strlen(s);
        int f = 1, v = 0;
        for (int i = 0; i < len; ++i) {
            if (s[i] == '(' || s[i] == '?')
                ++ v;
            else
                -- v;
            d[i] = v;
            if (v < 0) {
                f = 0;
                break;
            }
        }

        if ((len & 1) || (d[len - 1] & 1))
            f = 0;
        else if (f && d[len - 1] != 0) {
            for (int i = len - 2; i >= 0; --i)
                d[i] = std::min(d[i], d[i + 1]);

            v = d[len - 1] / 2;
            int cnt = 0;
            for (int i = 0; i < len && cnt != v; ++i) {
                if (s[i] != '?')
                    continue;
                if (d[i] - 2 * (cnt + 1) >= 0)
                    num[i] = ++cnt;
            }
            if (cnt != v) {
                f = 0;
            } else {
                int pre = 1;
                for (int i = 0; i < len && f != 2; ) {
                    if (num[i] != pre)
                        ++ i;
                    else {
                        int j;
                        for (j = i + 1; j < len; ++j) {
                            if (num[j] == pre + 1)
                                break;
                            if (s[j] != '?')
                                continue;
                            if (d[j] - pre * 2 >= 0) {
                                f = 2;
                                break;
                            }
                        }
                        i = j;
                        ++ pre;
                    }
                }
            }
        }
        if (f == 0)
            puts("None");
        else if (1 == f)
            puts("Unique");
        else
            puts("Many");
    }
    return 0;
}