hdu 4915 Parenthese sequence

Parenthese sequence

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


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
 
题意:?可以变成(或)存在括号配对的情况是无,唯一还是多种
解法:
第一遍预处理:只计算到i这个位置之前(和?号可以变成)的情况,如果一个问号可以变成)那么就把?变成),如果不行就变成(,一下三种情况
1. 有(在前面出现那么?可以变成)
2  (已经被匹配完了,那么?只能变成(--------最左边的?只能变成(
3 (被匹配完了,现在还遇到了),那么以前变成)的?就要变成(,那么就多出两个(,于是用一个(匹配当前)就可以了
如果(,?都没有,说明无解,当然字符串的长度为奇数,也必然无解
这一遍预处理可以得到的结果是是否存在解,以及没个位置得到的情况。

第二遍从右往左扫描,把?都与)进行匹配。
方法与第一遍一样,
然后得到一个?变成(的个数,以及)的个数
以这个字符的左边作为切割点,把两侧的(和)匹配完,如果有一侧的(或)多出来了,那么就把另一侧的变成括号的?变成另一种括号
现在只剩下这条切割线左右两侧分别变成)和(的?个数了。
如果数量都大于0,那么可以把这两个?都变成相反的括号,那么已然还是匹配的,这个时候就知道了括号匹配的结果不是唯一的了。

原因:
解存在的情况比较显然,
多解的情况是有两个?是连续的----连续的意思是中间的()?可以达成一个匹配,
而且这两个问号变成(),)(都是允许的,就存在多解。而且左边的?因为其左边有(所以之前变成),右边也是一样,所以改变方向不会出错
同时因为最左边和最右边的?都变成了相应的)(,于是这两个?改变是可行的,


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 1000007
int wen[maxn],kuo[maxn];
char word[maxn];
int work(int len){
    if(len % 2==1)return 0;
    int i;
    kuo[0] = wen[0] = 0;
    for(i=0;i<len;i++){
        if(i > 0)wen[i] = wen[i-1],kuo[i]=kuo[i-1];
        if(word[i]=='(')kuo[i]++;
        else if(word[i]=='?'){
            if(kuo[i]>0){wen[i]++;kuo[i]--;}
            else kuo[i]++;
        }
        else {
            if(kuo[i] > 0) kuo[i]--;
            else if(wen[i]>0)wen[i]--,kuo[i]++;
            else return 0;
        }
    }
    if(kuo[i-1] > 0) return 0;
    return 1;
}

int work2(int len){
    int i,a=0,b=0,c=0,d;
    for(i=len-1;i>0;i--){
        if(word[i]==')')a++;
        else if(word[i]=='?'){
            if(a>0)a--,b++;
            else a++;
        }
        else {
            if(a>0)a--;
            else if(b>0)a++,b--;
            else return 0;
        }
        c = a-kuo[i-1];
        if(c >= 0){
            d = b-c/2;
            if(d>0&&wen[i-1]>0)return 2;
        }
        else {
            d=wen[i-1]+c/2;
            if(d>0&&b>0)return 2;
        }
    }
    return 1;
}
int main(){
    while(scanf("%s",word)!=EOF){
        int len = strlen(word);
        int ans = work(len);
        if(ans == 0) {printf("None\n");continue;}
        ans = work2(len);
        if(ans == 1)printf("Unique\n");
        else printf("Many\n");
    }
    return 0;
}



/*

??
????
(??
((??))
?()?
?(?()?
?()(?)
((((
((()
(((?
((??
((?)
(()?
(())
(???
(??)
(?)?
(?))
))))
*/















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GDRetop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值