码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4915 Parenthese sequence(瞎搞题)

时间:2014-08-06 10:36:21      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   java   os   io   strong   for   

从左向右扫一遍左括号的最大值,与最小值。

从右向左扫一遍右括号的最大值,与最小值。

比较最大值中的最小数与最小中的最大数看能否有交集,0个,1个或者多个。

Parenthese sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 129


Problem Description
bobo found an ancient string. The string contains only three charaters -- "(", ")" and "?".

bobo would like to replace each "?" with "(" or ")" so that the string is valid (defined as follows). Check if the way of replacement can be uniquely determined.

Note:

An empty string is valid.
If S is valid, (S) is valid.
If U,V are valid, UV is valid.
 

Input
The input consists of several tests. For each tests:

A string s1s2…sn (1≤n≤106).
 

Output
For each tests:

If there is unique valid string, print "Unique". If there are no valid strings at all, print "None". Otherwise, print "Many".
 

Sample Input
?? ???? (??
 

Sample Output
Unique Many None
 

Author
Xiaoxu Guo (ftiasch)
 

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-12
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;


const int maxn = 1010000;

int sum[maxn][2], ans[maxn][2];
char str[maxn];

int main()
{
    while(cin >>str+1)
    {
        int len = strlen(str+1);
        if(len%2)
        {
            cout<<"None"<<endl;
            continue;
        }
        memset(sum, 0, sizeof(sum));
        memset(ans, 0, sizeof(ans));
        int flag = 0;
        for(int i = 1; i <= len; i++)
        {
            if(str[i] == '(')
            {
                sum[i][0] = sum[i-1][0]+1;
                sum[i][1] = sum[i-1][1]+1;
            }
            else if(str[i] == ')')
            {
                if(sum[i-1][0] == 0) sum[i][0] = 1;
                else sum[i][0] = sum[i-1][0]-1;
                sum[i][1] = sum[i-1][1] - 1;
            }
            else if(str[i] == '?')
            {
                if(sum[i-1][0] == 0) sum[i][0] = 1;
                else sum[i][0] = sum[i-1][0]-1;
                sum[i][1] = sum[i-1][1] +1;
            }
            if(sum[i][0] > sum[i][1]) flag = 1;
        }
        if(flag)
        {
            cout<<"None"<<endl;
            continue;
        }
        for(int i = len; i >= 1; i--)
        {
            if(str[i] == ')')
            {
                ans[i-1][0] = ans[i][0]+1;
                ans[i-1][1] = ans[i][1]+1;
            }
            else if(str[i] == '(')
            {
                if(ans[i][0] == 0) ans[i-1][0] = 1;
                else ans[i-1][0] = ans[i][0]-1;
                ans[i-1][1] = ans[i][1]-1;
            }
            else if(str[i] == '?')
            {
                if(ans[i][0] == 0) ans[i-1][0] = 1;
                else ans[i-1][0] = ans[i][0]-1;
                ans[i-1][1] = ans[i][1]+1;
            }
            if(ans[i][0] > ans[i][1]) flag = 1;
        }
        if(flag)
        {
            cout<<"None"<<endl;
            continue;
        }
        for(int i = 1; i <= len; i++)
        {
            int xx = max(sum[i][0], ans[i][0]);
            int yy = min(sum[i][1], ans[i][1]);
            if(xx > yy)
            {
                flag  = 1;
                break;
            }
            if(xx < yy)
                flag = 2;
        }
        if(flag == 1) cout<<"None"<<endl;
        else if(flag == 2) cout<<"Many"<<endl;
        else cout<<"Unique"<<endl;
    }
    return 0;
}


HDU 4915 Parenthese sequence(瞎搞题),布布扣,bubuko.com

HDU 4915 Parenthese sequence(瞎搞题)

标签:des   style   color   java   os   io   strong   for   

原文地址:http://blog.csdn.net/xu12110501127/article/details/38395775

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!