HDOJ 4915 Parenthese sequence



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 s 1s 2…s n (1≤n≤10 6).
 

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;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值