hdu 4915 Parenthese sequence

来源:互联网 发布:node.js webpack vue 编辑:程序博客网 时间:2024/05/21 10:44

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 s1s2…sn (1≤n≤106).
 

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

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

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


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;#define maxn 1000007int 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 0
原创粉丝点击