首页 > 其他 > 详细

HDOJ 4915 Parenthese sequence

时间:2014-08-12 10:23:23      阅读:328      评论:0      收藏:0      [点我收藏+]



Parenthese sequence

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


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)
 

Source
 



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

using namespace std;

const int maxn=1001000;

char str[maxn];
int num[maxn],prefix[maxn],suffix[maxn];
int pre0[maxn],suf0[maxn];

void init()
{
    memset(num,0,sizeof(num));
    memset(prefix,0,sizeof(prefix));
    memset(suffix,0,sizeof(suffix));
    memset(pre0,0,sizeof(pre0));
    memset(suf0,0,sizeof(suf0));
}

int main()
{
while(scanf("%s",str)!=EOF)
{
    int n=strlen(str);
    if(n%2==1)
    {
        puts("None"); continue;
    }
    init();
    for(int i=0;i<n;i++)
    {
        if(str[i]=='(') num[i+1]=1;
        else if(str[i]==')') num[i+1]=-1;
        else if(str[i]=='?') num[i+1]=0;
    }
    bool flag=true;
    for(int i=1;i<=n;i++)
    {
        if(num[i]) prefix[i]=prefix[i-1]+num[i];
        else prefix[i]=prefix[i-1]+1;
        if(prefix[i]<0)
        {
            flag=0; break;
        }
    }
    for(int i=n;i>=1;i--)
    {
        if(prefix[i]<=1)
            pre0[i]=pre0[i+1]+1;
        else pre0[i]=pre0[i+1];
    }
    if(flag==false)
    {
        puts("None"); continue;
    }
    for(int i=n;i>=1;i--)
    {
        if(num[i]) suffix[i]=suffix[i+1]-num[i];
        else suffix[i]=suffix[i+1]+1;
        if(suffix[i]<0)
        {
            flag=false;
            break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(suffix[i]<=1)
            suf0[i]=suf0[i-1]+1;
        else suf0[i]=suf0[i-1];
    }
    if(flag==false)
    {
        puts("None"); continue;
    }
    int cnt=0;
    for(int i=2;i<n;i++)
    {
        if(num[i]==0)
        {
            if( (prefix[i]>=2&&pre0[i]==0) && (suffix[i]>=2&&suf0[i]==0) )
                cnt++;
        }
    }
    if(cnt)
        puts("Many");
    else
        puts("Unique");
}
	return 0;
}



HDOJ 4915 Parenthese sequence,布布扣,bubuko.com

HDOJ 4915 Parenthese sequence

原文:http://blog.csdn.net/ck_boss/article/details/38510943

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!