hdu4915 Parenthese sequence 2014 Multi-University Training Contest 5

Parenthese sequence

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


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
 

Source
 

Recommend
hujie
 

比较麻烦的题,考虑多种情况,首先排除不行的,然后判断是否能有多个解,比赛的时候有个地方写错wa了5次···

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=1000005;
int f1[N],f2[N];
char s[N];
int main(){
    while(scanf("%s",s)!=EOF){
        int l=strlen(s);
        if(l%2){
            printf("None\n");
            continue;
        }
        if(s[0]==')'||s[l-1]=='('){
            printf("None\n");
            continue;
        }
        int flag=0;
        memset(f1,0,sizeof(f1));
        for(int i=1;i<l;i++){
            if(s[i]==')')f1[i]++;
            f1[i]+=f1[i-1];
            if(f1[i]>(i+1)/2){
                printf("None\n");
                flag=1;
                break;
            }
        }
        if(flag)continue;
        memset(f2,0,sizeof(f2));
        for(int i=l-2;i>=0;i--){
            if(s[i]=='(')f2[i]++;
            f2[i]+=f2[i+1];
            if((l-i)/2<f2[i]){
            printf("None\n");
            flag=1;
            break;
            }
        }
        if(flag)continue;
        int vis1=0,vis2=0;
        for(int i=l-1;i>=0;i--){
            if(f1[i]==(i+1)/2)break;
            if(s[i]=='?')vis1=i;
        }
        for(int i=0;i<l;i++){
            if(f2[i]==(l-i)/2)break;
            if(s[i]=='?')vis2=i;
        }
        if(vis1&&vis2&&vis2>vis1) printf("Many\n");
        else printf("Unique\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值