• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            pku1335 Digital Onion 遞歸

            題意:
            定義括號(hào)序列:
            Definition: Null parenthesis is a DO. Especially, we call this the Null DO. 
            Definition: "( )" is a DO. Especially, we call this the primitive DO. 
            Definition: If both A and B are DOs, then the combined form of "(A)B" is also a DO, where we call A the inside of the DO and B the outside of the DO. 
            定義排序規(guī)則:
            [Rule1]: The more weight, the more expensive. 
            [Rule2]: If the weights of two DOs are equal, then the price depends on the inside DO of the two DOs. 
            [Rule3]: If the weights of two DOs are the same and the prices of the inside DOs are equal, then the price depends on the outside DO of the two DOs. 
            給出一個(gè)符號(hào)序列,求這個(gè)之后的一個(gè)符號(hào)序列是什么

            思路:
            首先要確定最小的符號(hào)序列為:()()()...
            然后就是轉(zhuǎn)化方法:
            對(duì)于一個(gè)符號(hào)序列(inside)outside,首先看outside能否+1,不行的話看inside能否+1,同時(shí)將outside置為最小值形式,再不行的話當(dāng)outside不為空的時(shí)候?qū)nside的weight+1,outside's weight-1,將兩部分都轉(zhuǎn)化為最小值形式。
            以上描述的是一個(gè)遞歸過(guò)程~當(dāng)然,考慮一種特殊情況,如果整個(gè)式子無(wú)法進(jìn)行+1操作,只能將整個(gè)式子的weight+1,然后化為最小值形式。

            發(fā)現(xiàn)java的string竟然是值傳遞,非常的蛋疼。。我喜歡java的String,沒辦法,只好用C++的string。。。這種字符串處理的題目string是非常給力的~

            代碼:
             1# include <iostream>
             2# include <string>
             3# include <cstring>
             4using namespace std;
             5string str;
             6void make(int s,int e,int type)
             7{
             8    int c=0;
             9    for(int i=s;i<e;i++)
            10        if(str[i]=='(')
            11            c++;
            12    c+=type;
            13    string res="";
            14    for(int i=0;i<c;i++) res+="()";
            15    str=str.substr(0,s)+res+str.substr(e,str.length()-e);
            16}

            17bool solve(int s,int e)
            18{
            19    if(s==e) return false;
            20    else
            21    {
            22        int c=-1,end;
            23        for(end=s+1;end<e&&c;end++)
            24            switch(str[end])
            25            {
            26            case '(':c--;break;
            27            case ')':c++;break;
            28            }
            ;
            29        if(solve(end,e)) return true;
            30        else if(solve(s+1,end-1))
            31        {
            32            make(end,e,0);
            33            return true;
            34        }

            35        else if(end!=e)
            36        {
            37            make(end,e,-1);
            38            make(s+1,end-1,1);
            39            return true;
            40        }

            41        else return false;
            42    }

            43}

            44int main()
            45{
            46    int test;
            47    cin>>test;
            48    while(test--)
            49    {
            50        str.clear();
            51        while(true)
            52        {
            53            string tmp;
            54            cin>>tmp;
            55            if(tmp=="$"break;
            56            str+=tmp;
            57        }

            58        if(!solve(0,str.length())) make(0,str.length(),1);
            59        for(int i=0;i<str.length();i++)
            60            cout<<str[i]<<" ";
            61        cout<<"$"<<endl;
            62    }

            63    return 0;
            64}

            posted on 2011-01-25 01:01 yzhw 閱讀(258) 評(píng)論(0)  編輯 收藏 引用 所屬分類: DP

            <2011年1月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            導(dǎo)航

            統(tǒng)計(jì)

            公告

            統(tǒng)計(jì)系統(tǒng)

            留言簿(1)

            隨筆分類(227)

            文章分類(2)

            OJ

            最新隨筆

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            亚洲国产天堂久久综合网站| 一97日本道伊人久久综合影院 | 久久综合丝袜日本网| www亚洲欲色成人久久精品| 欧美激情精品久久久久久久九九九| 久久最新免费视频| 国内精品久久久久久久97牛牛| 久久综合欧美成人| 色婷婷综合久久久中文字幕| 99久久精品无码一区二区毛片| 中文字幕无码精品亚洲资源网久久| 91精品国产91久久久久久青草| 亚洲国产精品久久久天堂| 精品久久人人做人人爽综合| 国产精品国色综合久久| 久久人人添人人爽添人人片牛牛| 狠狠色丁香久久综合婷婷| 亚洲乱码中文字幕久久孕妇黑人| 久久激情五月丁香伊人| 久久电影网一区| 99国产精品久久| 久久天天躁狠狠躁夜夜躁2O2O| 久久婷婷五月综合国产尤物app | 日韩久久无码免费毛片软件| 成人a毛片久久免费播放| 久久精品嫩草影院| 久久99精品综合国产首页| 韩国免费A级毛片久久| 99国产欧美久久久精品蜜芽| 久久精品蜜芽亚洲国产AV| 色妞色综合久久夜夜| 性色欲网站人妻丰满中文久久不卡| 污污内射久久一区二区欧美日韩 | 欧美一区二区三区久久综| 久久婷婷人人澡人人爽人人爱 | 99久久中文字幕| 久久综合久久综合久久综合| 久久久久中文字幕| 日韩十八禁一区二区久久| 老男人久久青草av高清| 久久精品欧美日韩精品|