HDU4915:Parenthese sequence(贪心)

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

从前扫一遍,记录最大和最小的可能的左括号的值

如果左括号的大小小于右括号的大小,则必定不行

从后扫一遍,同样记录最大和最小的可能的左括号的值

如果右括号的大小小于左括号的大小,则必定不行

然后找最小值中的最大值,和最大值中的最小值

如果前者大于后者,则不行,如果后者大于前者则有多重解,如果相等则为唯一解

嗯,发现我代码越来越飘逸了,有木有

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define mem(a,b) memset(a,0,sizeof(a))
#define L 1000005
#define up(i,x,y) for(i = x;i<=y;i++)
#define down(i,x,y) for(i = x;i>=y;i--)
#define bre(x,y) if(x){flag=y;break;}
#define con(x,y) if(x){ans(y);continue;}
char s[L];
int a[L][2],b[L][2],len,flag;

void ans(int i)
{
    if(i==1) printf("Unique\n");
    else if(i==2) printf("Many\n");
    else printf("None\n");
}

int main()
{
    int i,j;
    while(~scanf("%s",s+1))
    {
        len = strlen(s+1);
        con((len&1),0)
        mem(a,0);
        mem(b,0);
        flag = 1;
        up(i,1,len)
        {
            if(s[i]=='(')
                a[i][0]=a[i-1][0]+1,a[i][1]=a[i-1][1]+1;
            else if(s[i]==')')
            {
                if(!a[i-1][0])
                    a[i][0]=1;
                else
                    a[i][0]=a[i-1][0]-1;
                a[i][1]=a[i-1][1]-1;
            }
            else if(s[i]=='?')
            {
                if(!a[i-1][0]) a[i][0]=1;
                else
                    a[i][0]=a[i-1][0]-1;
                a[i][1]=a[i-1][1]+1;
            }
            bre((a[i][0]>a[i][1]),0)
        }
        con((!flag),0)
        down(i,len,1)
        {
            if(s[i]==')')
                b[i-1][0]=b[i][0]+1,b[i-1][1]=b[i][1]+1;
            else if(s[i]=='(')
            {
                if(!b[i][0])
                    b[i-1][0]=1;
                else
                    b[i-1][0]=b[i][0]-1;
                b[i-1][1]=b[i][1]-1;
            }
            else if(s[i]=='?')
            {
                if(!b[i][0]) b[i-1][0]=1;
                else
                    b[i-1][0]=b[i][0]-1;
                b[i-1][1]=b[i][1]+1;
            }
            bre((b[i][0]>b[i][1]),0)
        }
        con((!flag),0)
        int l,r;
        flag = 1;
        up(i,1,len)
        {
            l = max(a[i][0],b[i][0]);
            r = min(a[i][1],b[i][1]);
            bre((l>r),0)
            bre((l<r),2)
        }
        con((flag==0),0);
        con((flag==1),1);
        con((flag==2),2);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值