演算法導論15.4-3

來源:互聯網
上載者:User

  動態規劃的備忘錄方法.這是我第一次使用備忘錄方法,說白了就是遞迴地調用,之後保留一個表,導致沒有重複的調用出現.很不錯.

// memoizedLCSLenth.cpp -- 2011-06-18-02.57<br />#include "stdafx.h"<br />#include <iostream><br />#include <cstring></p><p>const int EMPTY = -1 ;</p><p>void LCSLenth (const char * const strA, const char * const strB) ;<br />int subProgram (int * const * const memoized, const char * const strA, const char * const strB, const int i, const int j) ;<br />void introductionToAlgorithms15_4_2 (const int * const * const memoized, const char * const strA, const char * const strB, const int i, const int j) ;</p><p>int _tmain(int argc, _TCHAR* argv[])<br />{<br />char * strA = " ABCBDAB", * strB = " BDCABA" ;</p><p>LCSLenth(strA, strB) ;</p><p>return 0;<br />}</p><p>//Assume imports two strings, first position of each string dosen't storage character.<br />void LCSLenth (const char * const strA, const char * const strB)<br />{<br />int aSize = strlen(strA) ;<br />int * * memoized = new int *[aSize] ;<br />int bSize = strlen(strB) ;<br />for (int i = 0; i < aSize; ++i)<br />memoized[i] = new int[bSize] ;<br />for (int i = 0; i < aSize; ++i)<br />{<br />for (int j = 0; j < bSize; ++j)<br />memoized[i][j] = EMPTY ;<br />}<br />subProgram(memoized, strA, strB, aSize - 1, bSize - 1) ;<br />introductionToAlgorithms15_4_2(memoized, strA, strB, aSize - 1, bSize - 1) ;<br />for (int i = 0; i < aSize; ++i)<br />delete []memoized[i] ;<br />delete []memoized ;<br />}</p><p>int subProgram (int * const * const memoized, const char * const strA, const char * const strB, const int i, const int j)<br />{<br />if (0 == i || 0 == j)<br />{<br />if (EMPTY == memoized[i][j])<br />memoized[i][j] = 0 ;<br />}<br />else if (strA[i] == strB[j])<br />{<br />if (EMPTY == memoized[i - 1][j - 1])<br />memoized[i - 1][j - 1] = subProgram(memoized, strA, strB, i - 1, j - 1) ;<br />memoized[i][j] = memoized[i - 1][j - 1] + 1 ;<br />}<br />else<br />{<br />if (EMPTY == memoized[i - 1][j])<br />memoized[i - 1][j] = subProgram(memoized, strA, strB, i - 1, j) ;<br />if (EMPTY == memoized[i][j - 1])<br />memoized[i][j - 1] = subProgram(memoized, strA, strB, i, j - 1) ;<br />memoized[i][j] = memoized[i - 1][j] >= memoized[i][j - 1] ? memoized[i - 1][j] : memoized[i][j - 1] ;<br />}</p><p>return memoized[i][j] ;<br />}</p><p>void introductionToAlgorithms15_4_2 (const int * const * const memoized, const char * const strA, const char * const strB, const int i, const int j)<br />{<br />if (0 == i || 0 == j)<br />return ;<br />if (strA[i] == strB[j])<br />{<br />introductionToAlgorithms15_4_2(memoized, strA, strB, i - 1, j - 1) ;<br />std ::cout << strA[i] ;<br />}<br />else if (memoized[i - 1][j] >= memoized[i][j - 1])<br />introductionToAlgorithms15_4_2(memoized, strA, strB, i - 1, j) ;<br />else<br />introductionToAlgorithms15_4_2(memoized, strA, strB, i, j - 1) ;<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.