• <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>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            PKU 1159 Palindrome

            問題:
            http://acm.pku.edu.cn/JudgeOnline/problem?id=1159

            思路:
            看完題目,完全不知道該怎么做
            參考discuss,發現原來DP這么這么地強大,自己那是相當菜
            設ch[1]..ch[n]表示字符串1至n位,i為左游標,j為右游標 ,則i從n遞減,j從i開始遞增。
            min[i][j]表示i和j之間至少需要插入多少個字符才能對稱,初始置全0 ,我們最終需要得到的值是min[1][n].
            則
            if(ch[i]==ch[j])                                    //如果兩個游標所指字符相同,向中間縮小范圍
                min[i][j]=min[i+1][j-1];
            else
                min[i][j] = 1 +(min[i+1][j]和min[i][j-1]中的較小值);     //如果不同,典型的狀態轉換方程
            額,狀態方程看明白了,結果發現代碼居然不知道怎么寫...那個汗哪...
            原因是沒有發現當i>j時,min[i][j] = 0

            代碼(記憶化搜索):
             1 /* Time: 1594MS */
             2 #include<stdio.h>
             3 #include<stdlib.h>
             4 #include<string.h>
             5 #define MAX_LEN 5001
             6 char str[MAX_LEN+1];
             7 int len;
             8 short memory[MAX_LEN][MAX_LEN];
             9 
            10 /*
            11  * f[i][j] represent the minimal insert for str[i..j]
            12  *
            13  * f[i][j] = f[i+1][j-1], if str[i]==str[j], otherwise
            14  * f[i][j] = min(f[i+1][j], f[i][j-1]) + 1
            15  */
            16 int
            17 func(int i, int j)
            18 {
            19     int a, b;
            20     if(i >= j)
            21         return 0;
            22     if(memory[i][j])
            23         return memory[i][j];
            24     if(str[i] == str[j])
            25         memory[i][j] = func(i+1, j-1);
            26     else {
            27         a = func(i+1, j);
            28         b = func(i, j-1);
            29         memory[i][j] = a<? a+1 : b+1;
            30     }
            31     return memory[i][j];
            32 }
            33 
            34 int
            35 main(int argc, char **argv)
            36 {
            37     while(scanf("%d"&len) != EOF) {
            38         memset(memory, 0sizeof(memory));
            39         scanf("%s", str+1);
            40         printf("%d\n", func(1, len));
            41     }
            42 }

            代碼(dp):
             1 #include<stdio.h>
             2 #include<stdlib.h>
             3 #include<string.h>
             4 #define MAX_LEN 5001
             5 char str[MAX_LEN];
             6 int len;
             7 short table[MAX_LEN][MAX_LEN];
             8 
             9 short
            10 dp()
            11 {
            12     int i, j;
            13     /* if i>j, then table[i][j] = 0 */
            14     for(i=len-1; i>=1; i--) {
            15         for(j=i; j<=len; j++) {
            16             if(str[i] == str[j])
            17                 table[i][j] = table[i+1][j-1];
            18             else
            19                 table[i][j] = table[i+1][j]<table[i][j-1? table[i+1][j]+1 : table[i][j-1]+1;
            20         }
            21     }
            22     return table[1][len];
            23 }
            24 
            25 int
            26 main(int argc, char **argv)
            27 {
            28     while(scanf("%d"&len) != EOF) {
            29         memset(table, 0sizeof(table));
            30         scanf("%s", str+1);
            31         printf("%d\n", dp());
            32     }
            33 }

            posted on 2010-08-12 19:47 simplyzhao 閱讀(156) 評論(0)  編輯 收藏 引用 所屬分類: C_動態規劃

            導航

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久国产精品人妻| 久久九九亚洲精品| 久久久久女教师免费一区| 久久精品视频91| 久久久久久精品成人免费图片| 久久综合亚洲欧美成人| 九九久久精品无码专区| 狠狠色丁香久久婷婷综合| 亚洲国产成人久久精品动漫| 欧美伊人久久大香线蕉综合69| 色偷偷88888欧美精品久久久| 精品久久久无码中文字幕天天| 久久人人爽人人爽人人爽| 久久最新精品国产| 精品国产乱码久久久久软件| 国产香蕉97碰碰久久人人| 亚洲熟妇无码另类久久久| 欧美久久综合九色综合| 日本免费一区二区久久人人澡| 亚洲女久久久噜噜噜熟女| 人人狠狠综合久久亚洲高清| 久久免费精品一区二区| 久久国产精品成人影院| 亚洲精品无码久久千人斩| 久久综合久久鬼色| 久久综合九色欧美综合狠狠| 久久精品国产精品亚洲精品| 国内精品久久久久久99蜜桃| 日韩精品久久无码中文字幕| 久久久无码精品亚洲日韩京东传媒 | 精品久久久久久亚洲| 一本色道久久99一综合| 久久午夜免费视频| 婷婷久久综合| 久久精品桃花综合| 亚洲综合精品香蕉久久网| 久久久久久精品无码人妻| 亚洲中文字幕无码久久2017| 久久婷婷五月综合97色直播| 成人久久免费网站| 亚洲AV日韩AV天堂久久|