(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 />