<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Change Dir

    先知cd——熱愛(ài)生活是一切藝術(shù)的開(kāi)始

    統(tǒng)計(jì)

    留言簿(18)

    積分與排名

    “牛”們的博客

    各個(gè)公司技術(shù)

    我的鏈接

    淘寶技術(shù)

    閱讀排行榜

    評(píng)論排行榜

    深入理解動(dòng)態(tài)規(guī)劃的一系列問(wèn)題(12)

    今天介紹三個(gè)經(jīng)典問(wèn)題,為什么直接來(lái)三個(gè)?大概是因?yàn)檫@三個(gè)問(wèn)題我覺(jué)得太經(jīng)典了,以至于在任何人學(xué)習(xí)任何語(yǔ)言時(shí)都可能要實(shí)現(xiàn)這些題目的編程訓(xùn)練,所以這里就放到一起,打包講下求解這些問(wèn)題的動(dòng)態(tài)規(guī)劃思路(雖然有些問(wèn)題不是那么直觀的動(dòng)態(tài)規(guī)劃解)。

    第一個(gè)問(wèn)題是編輯距離(Edit Distance Problem)EDP問(wèn)題,這個(gè)問(wèn)題在維基上有全面的解釋,并附有準(zhǔn)確的代碼實(shí)現(xiàn)(也叫l(wèi)evenshtein距離),我這里就簡(jiǎn)單講下遞歸的DP解法。編輯距離需要一些前置條件,首先是距離的定義:對(duì)于兩個(gè)字符串x和y,其中x包含m個(gè)字符,y包含n個(gè)字符,目標(biāo)是由x串經(jīng)過(guò)一系列允許的操作后變到y(tǒng)串,這些操作包括:1)刪除操作D,刪掉任意一個(gè)字符,成本為c(D);2)插入操作I,插入任意一個(gè)字符,成本為c(I);3)替換操作R,替換掉字符串中的一個(gè)字符,成本為c(R)。當(dāng)然變?yōu)閥串后要求變化成本最優(yōu)。這就是一個(gè)典型的DP問(wèn)題了。定義狀態(tài)(i,j)為x串的前i個(gè)字符(即i串前綴),y串的j串前綴,于是DPFE為:image 。換種表達(dá)就是image ,轉(zhuǎn)移函數(shù)t表示如下:image 。目標(biāo)就是計(jì)算f(x,y)。

    舉例來(lái)說(shuō):x="CAN”,y=”ANN”,三種操作的成本均為1,求x和y的編輯距離。(答案為2,具體求解見(jiàn)下面代碼)

    源碼求解:

       1: /*
       2:  * Copyright (C) 2013 changedi
       3:  *
       4:  * Licensed under the Apache License, Version 2.0 (the "License");
       5:  * you may not use this file except in compliance with the License.
       6:  * You may obtain a copy of the License at
       7:  *
       8:  * http://www.apache.org/licenses/LICENSE-2.0
       9:  *
      10:  * Unless required by applicable law or agreed to in writing, software
      11:  * distributed under the License is distributed on an "AS IS" BASIS,
      12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13:  * See the License for the specific language governing permissions and
      14:  * limitations under the License.
      15:  */
      16: package com.jybat.dp;
      17:  
      18: //EditDistanceProblem;
      19: public class EDP {
      20:  
      21:     private static String s1 = "CAN";
      22:     private static String s2 = "ANN";
      23:     private static final int insertionCost = 1;
      24:     private static final int deletionCost = 1;
      25:     private static final int replacementCost = 1;
      26:     // it is useful to have the string lengths as symbolic constants
      27:     private static int s1Length = s1.length();
      28:     private static int s2Length = s2.length();
      29:  
      30:     // The decision space is constant in this example.
      31:     // It does not depend on the current state.
      32:     // We chose to code the 3 allowed operations
      33:     // as follows:
      34:     // 1 stands for delete
      35:     // 2 stands for insert
      36:     // 12 stands for replace
      37:     private static int[] decisionSet = { 1, 2, 12 };
      38:  
      39:     // costOfOperation()
      40:     // returns 0 if the specified characters in the 2 strings
      41:     // match and the decision is to perform
      42:     // "replacement" operation for matching chars
      43:     // (whose cost is usually defined as 0)
      44:     // returns 1 otherwise (if delete, insert, or a real replacement operation
      45:     // happens)
      46:     private static int costOfOperation(int i, int j, int dec) {
      47:         if (dec == 12) { // dec==12 means decision is to replace
      48:             if (s1.charAt(i - 1) // note: subtract 1 because array
      49:             // starts at index 0
      50:             == s2.charAt(j - 1)) { // matching chars, cost is 0
      51:                 return 0;
      52:             } else { // real replacement
      53:                 return replacementCost; // cost of real replacement
      54:             }
      55:         }
      56:         if (dec == 1) { // dec==1 means decision is to delete
      57:             return deletionCost;
      58:         }
      59:         // otherwise it must be that dec==2, decision to insert
      60:         return insertionCost;
      61:     }
      62:  
      63:     private static int s1Adjustment(int dec) {
      64:         if (dec == 2) { // insert
      65:             return 0;
      66:         }
      67:         return 1;
      68:     }
      69:  
      70:     private static int s2Adjustment(int dec) {
      71:         if (dec == 1) { // delete
      72:             return 0;
      73:         }
      74:         return 1;
      75:     }
      76:  
      77:     public static int d(int i, int j) {
      78:         if (i == 0)
      79:             return j;
      80:         if (j == 0)
      81:             return i;
      82:         int min = Integer.MAX_VALUE;
      83:         for (int dec : decisionSet) {
      84:             int t = costOfOperation(i, j, dec)
      85:                     + d(i - s1Adjustment(dec), j - s2Adjustment(dec));
      86:             if (t < min)
      87:                 min = t;
      88:         }
      89:         return min;
      90:     }
      91:  
      92:     /**
      93:      * @param args
      94:      */
      95:     public static void main(String[] args) {
      96:         System.out.println(d(s1Length, s2Length));
      97:  
      98:     }
      99:  
     100: }

    第二個(gè)問(wèn)題是大家非常熟悉的斐波那契數(shù)列問(wèn)題Fibonacci Recurrence Relation (FIB):

    斐波那契數(shù)列就是一個(gè)經(jīng)典的f(n)=f(n-1)+f(n-2)的遞歸問(wèn)題。這里不講,只是列出這個(gè)問(wèn)題。

    第三個(gè)問(wèn)題是漢諾塔問(wèn)題Tower of Hanoi Problem (HANOI):

    漢諾塔的問(wèn)題描述就省去了,主要是定義狀態(tài),我們定義在移動(dòng)了i個(gè)碟子后的最優(yōu)步數(shù)為f(i),則f(i)=2f(i-1)+1,即通用的漢諾塔策略:將碟子先移動(dòng)到中間柱,移動(dòng)i-1個(gè)后,將第i個(gè)碟子移動(dòng)到目標(biāo)柱,然后再重復(fù)將i-1個(gè)柱子以同樣的方法放回目標(biāo)柱,于是這個(gè)dp方程就可以理解了。但是這樣子的方程看上去總不像動(dòng)態(tài)規(guī)劃那明細(xì)的階段和狀態(tài)的對(duì)應(yīng),于是我們修正一下方程如下:f(m,i,j,k)=opt {f(m-1,i,k,j)+f(1,i,j,k)+f(m-1,k,j,i)}。狀態(tài)(m,i,j,k)表示把m個(gè)碟子從i移動(dòng)到j(luò)的最優(yōu)步數(shù),k代表中間那個(gè)柱。

    DP源碼如下:

     

       1: /*
       2:  * Copyright (C) 2013 changedi
       3:  *
       4:  * Licensed under the Apache License, Version 2.0 (the "License");
       5:  * you may not use this file except in compliance with the License.
       6:  * You may obtain a copy of the License at
       7:  *
       8:  * http://www.apache.org/licenses/LICENSE-2.0
       9:  *
      10:  * Unless required by applicable law or agreed to in writing, software
      11:  * distributed under the License is distributed on an "AS IS" BASIS,
      12:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13:  * See the License for the specific language governing permissions and
      14:  * limitations under the License.
      15:  */
      16: package com.jybat.dp;
      17:  
      18: //tower of hanoi problem
      19: //Compute the number of moves when the recursive
      20: //strategy is used. 
      21: public class HANOI {
      22:  
      23:     // m: number of disks to move
      24:     // i: index of source tower
      25:     // j: index of destination tower
      26:     // k: index of temporary tower
      27:     public static int f(int m, int i, int j, int k) {
      28:         if (m == 1)
      29:             return 1;
      30:         return f(m - 1, i, k, j) + f(1, i, j, k) + f(m - 1, k, j, i);
      31:     }
      32:  
      33:     /**
      34:      * @param args
      35:      */
      36:     public static void main(String[] args) {
      37:         System.out.println(f(3,1,2,3));
      38:     }
      39:  
      40: }

    總結(jié)下,其實(shí)這三個(gè)問(wèn)題,除了編輯距離外,fib和hanoi都更像是遞歸教學(xué)而不是DP,但是其實(shí)從問(wèn)題的結(jié)構(gòu)上講,hanoi這樣的是典型的動(dòng)態(tài)規(guī)劃求解,只不過(guò)過(guò)多的教科書(shū)和經(jīng)驗(yàn)把它們放到了遞歸里,同時(shí)又忽略了遞歸和DP的聯(lián)系。所以我這里把它們放到一起,用來(lái)說(shuō)明動(dòng)態(tài)規(guī)劃問(wèn)題也包含經(jīng)典的遞歸問(wèn)題的。

    posted on 2014-05-27 10:38 changedi 閱讀(1870) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 算法

    主站蜘蛛池模板: 亚洲AV成人无码久久精品老人| 亚洲国产中文在线视频| 国产精品亚洲一区二区麻豆| 美女被吸屁股免费网站| 午夜免费啪视频在线观看| 亚洲成AV人在线观看网址| 亚洲va中文字幕无码久久 | 亚洲中文字幕在线第六区| 一级大黄美女免费播放| 亚洲高清成人一区二区三区| 四虎国产精品成人免费久久| 91九色精品国产免费| 亚洲一区二区三区在线播放| 亚洲fuli在线观看| 午夜a级成人免费毛片| 亚洲国产精品ⅴa在线观看| 免费A级毛片无码A| a级在线观看免费| 亚洲精品私拍国产福利在线| 特级毛片aaaa级毛片免费| 亚洲精品成人区在线观看| 亚洲日产乱码一二三区别| 爽爽日本在线视频免费| 四虎精品免费永久免费视频| 亚洲夜夜欢A∨一区二区三区| 亚洲人成网亚洲欧洲无码| 蜜臀98精品国产免费观看| 久久亚洲精品国产亚洲老地址| 99在线观看视频免费| 亚洲中文字幕在线第六区| 亚洲黄页网在线观看| 18禁止看的免费污网站| 亚洲精品又粗又大又爽A片| 可以免费看黄的网站| 777亚洲精品乱码久久久久久 | 99久久99久久精品免费观看 | 在线观看国产情趣免费视频| 亚洲福利中文字幕在线网址| 久99久精品免费视频热77| 亚洲精品国产精品乱码不99| 一级一片免费视频播放|