演算法導論15.4-4

來源:互聯網
上載者:User

(min (m, n))空間求出最長公用子序列的長度.這個問題琢磨了好久,終於還是睾出來了.   實現的思想就是,從兩個串中選出一個較長的串.用另外一個串的所有,同較長串的當前值比較.只記錄最大的長度,最終完成.   說的,比較簡單.研究過程,還算一般.全下來大約4小時.是慢還是快?貌似很慢啊,希望自己今後養成高速解決問題的習慣吧.   還有啊,網吧是在是太吵了...

 

 

// 15-4-4.cpp -- 2011-06-18-15.33<br />// Completeed at 2011-06-19-10.26<br />#include "stdafx.h"<br />#include <iostream></p><p>int maxLenth (const char * const strA, const char * const strB) ;</p><p>int _tmain(int argc, _TCHAR* argv[])<br />{<br />char * strA = " ABCDBABCBABDA", * strB = " ABDBAABABCBDBABCDABA" ;<br />std ::cout << maxLenth(strA, strB) << std ::endl ;</p><p>return 0 ;<br />}<br />// Assume aSize <= bSize<br />int maxLenth (const char * const strA, const char * const strB)<br />{<br />int aSize = strlen(strA) ;<br />int bSize = strlen(strB) ;<br />int * maxLenth = new int[aSize] ;</p><p>if (!maxLenth)<br />return -1 ;<br />for (int i = 0; i < aSize; ++i)<br />maxLenth[i] = 0 ;<br />for (int i = 0; i < bSize; ++i)<br />{<br />if (strB[i] == strA[0])<br />maxLenth[0] = 1 ;<br />else<br />maxLenth[0] = 0 ;<br />for (int j = 1; j < aSize; ++j)<br />{<br />if (strB[i] == strA[j])<br />{<br />int max = 0 ;<br />for (int k = j - 1; k >= 0; --k)<br />{<br />if (maxLenth[k] < i + 1 && maxLenth[k] > max)<br />max = maxLenth[k] ;<br />}<br />if (max == maxLenth[j])<br />maxLenth[j] = maxLenth[j] + 1 ;<br />else if (max + 1 < maxLenth[j])<br />continue ;<br />else maxLenth[j] = max + 1 ;<br />}<br />}<br />}<br />int max = maxLenth[aSize - 1] ;<br />delete []maxLenth ;</p><p>return max ;<br />}<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.