HDU4915 Parenthese sequence

26 篇文章 0 订阅

Parenthese sequence

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


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
 

Recommend

先处理出问号数,左括号数,右括号数,问号就是先补齐左右括号的差值,剩下的再平均分.
知道了要补几个左括号几个右括号之后,先把左括号放到最左边的问号位置,右括号放到最右边的问号位置.
扫一遍如果合理,至少有一解.
把最后一个用左括号替换的问号和第一个用右括号替换的问号交换,若合理,则多解.

#include<iostream>
#include<string>
#include<set>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAXN=1000000+10;
string s;
string t;
stack<int> a;
int pre[MAXN];
int len;
int check(){
    int n=len;
    int num=0;
    for(int i=0;i<n;i++){
        if(t[i]=='(')
            num++;
        else{
            num--;
            if(num<0)
                return 0;
        }
    }
    if(num>0)
        return 0;
    return 1;
}

int main(){
   // freopen("data.txt","r",stdin);
   // freopen("f.txt","w",stdout);
    t.resize(MAXN);
    while(cin>>s){
        len=s.length();
        int s1=0,s2=0;
        if(len&1){
            puts("None");
            continue;
        }
        int x=0,y=0,z=0;
        for(int i=0;i<len;i++){
            if(s[i]=='(')
                x++;
            else if(s[i]==')')
                y++;
            else
                z++;
        }
        if(x-y>z||y-x>z){
            puts("None");
            continue;
        }
        int xx=(z-x+y)/2;
        int yy=(z+x-y)/2;
        for(int i=0;i<len;i++){
            if(s[i]=='?'){
                if(s1<xx){
                    t[i]='(';
                    s1++;
                }
                else
                    t[i]=')';
            }
            else
                t[i]=s[i];
        }
        int num=0;
        if(!check()){
            puts("None");
            continue;
        }
        if(xx==z||yy==z){
            puts("Unique");
            continue;
        }
        int ok=0;
        int ans=0;
        for(int i=0;i<len;i++){
            if(s[i]=='?'&&ok==xx){
                swap(t[i],t[ans]);
                break;
            }
            if(s[i]=='?'&&ok==xx-1){
                ok++;
                ans=i;
            }else if(s[i]=='?'){
                ok++;
            }
        }
        if(check()){
            puts("Many");
        }else{
            puts("Unique");
        }
    }
}














 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值