hdu 4915 Parenthese sequence,hduparenthese

來源:互聯網
上載者:User

hdu 4915 Parenthese sequence,hduparenthese
Parenthese sequenceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 50    Accepted Submission(s): 11


Problem Descriptionbobo 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.
 
InputThe input consists of several tests. For each tests:

A string s1s2…sn (1≤n≤106). 
OutputFor 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
 
AuthorXiaoxu Guo (ftiasch) 
Source2014 Multi-University Training Contest 5 



題解及代碼:


#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;struct node{    int pos,l,r;  //用來記錄問號的位置和正序(從左至右)時,}t[1000100];      //左括弧的最大數目和右括弧的數目int main(){    int k=0;    char s[1000100];    memset(s,0,sizeof(s));    while(scanf("%s",&s)!=EOF)    {        k=1;        int nl=0,nr=0;        bool flag=true;        int len=strlen(s);        if(len%2)         //長度為奇數時,不可能        {            printf("None\n");            continue;        }        int l=0,r=0;        for(int i=0;i<len;i++)        //從左向右測試,看是否能出現()))……這種情況        {                             //我們把?都當作左括弧來處理            if(s[i]=='('||s[i]=='?') l++;            else r++;            if(r>l)            {                flag=false;                break;            }            if(s[i]=='?')            {                t[k].pos=i;t[k].l=l;t[k++].r=r;            }            else if(s[i]=='(') nl++;            else nr++;        }        if(!flag)        {            printf("None\n");            continue;        }        l=0,r=0;        for(int i=len-1;i>=0;i--)         //從右向左測試是否會出現……(((()的情況        {                                 //我們把?都當作右括弧來處理            if(s[i]==')'||s[i]=='?') r++;            else l++;            if(r<l)            {                flag=false;                break;            }        }        if(!flag)        {            printf("None\n");            continue;        }                                              //排除了上述的不可能情況之後,剩下的一定能構造出來了        int n=len/2;                    //下面討論unique和many的區別        if(nl==n||nr==n)                //nl記錄左括弧的數目,nr記錄右括弧的數量,這裡unique很好理解        {            printf("Unique\n");            continue;        }        flag=false;                    //這裡的解釋放在最下面        int i=n-nl;        if(i<k-1)        {            int j=i+1,d=-1;            l=t[i].l-1;            r=t[i].r+1;            if(l>=r)            for(d=t[i].pos+1;d<t[j].pos;d++)            {                if(s[d]=='(') l++;                else if(s[d]==')') r++;                if(r>l) break;            }            if(d>=t[j].pos) flag=true;        }        if(flag) printf("Many\n");        else printf("Unique\n");    }}/*這裡解釋一下能構造出括弧對的情況下,unique和many的區別。首先我們能想到,如果只有一個問號或者沒有問號的情況下,那麼這就只能是unique了。然後考慮問號的數目大於等於2的情況:(這裡我們只考慮正序)如果我想要得到many,首先我們要保證一對問號變換之後,如:?……?變成(……),還能將這對括弧對換位置,即)……(。那麼這樣改過之後,我們可能就會出現())))……的情況,如果出現這種情況,那麼我們選的這對?……?就是不能變動的,接著選取另外兩對問號進行測試,直到測試完所有的問號對都出現上述情況,那就只能是unique或者當我們選定的問號對不會出現()))……的情況就能輸出many了。其實這樣已經計算了所有能夠達到many的情況,其實我們只要計算一個就可以了,那麼我們計算哪一個呢?挑一個對整體影響最小的,即我們把前n-nl-1個問號都變成(,本來第n-nl個我們也變成(的,那麼剩下的變成)就能滿足情況了,這裡我們把它變成),而第n-nl+1個問號變成(,即在最原始的情況下(左括弧都在右括弧的左邊)把最中間兩個??從()變成)(,這樣影響是最小的。為什麼呢?比如:((??)(????)),當我們正序考慮時,貪心思想先把遇到的?變成左括弧,然後是其他的變成右括弧,這樣我們判斷第二種不滿足條件時才能儘可能的使其滿足情況。假設我們把第一個?變成),那麼我們判斷時,它影響當前位置及後面所有位置的l和r的數目;當我們把將第三個?變成)時,它影響的只有當前位置和它後面所有位置l和r的數目。同時我們也能發現,當後一種情況都不能滿足many時,前面的情況是根本不可能構成many的。*轉載請註明出處,謝謝。*/





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.