{"id":1950,"date":"2016-04-05T12:37:58","date_gmt":"2016-04-05T04:37:58","guid":{"rendered":"http:\/\/boweihe.me\/?p=1950"},"modified":"2016-04-05T12:37:58","modified_gmt":"2016-04-05T04:37:58","slug":"longest-common-subsequence-%e6%9c%80%e9%95%bf%e5%85%ac%e5%85%b1%e5%ad%90%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1950","title":{"rendered":"Longest Common Subsequence \u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217"},"content":{"rendered":"<p>\u8fd9\u662f\u4e2a\u5f88\u8001\u7684\u95ee\u9898\u4e86\uff0c\u6700\u8fd1\u603b\u662f\u78b0\u5230\uff0c\u5927\u4e09\u65f6\u5019\u5b66\u7684\u7b97\u6cd5\u597d\u591a\u90fd\u5fd8\u8bb0\u4e86\uff0c\u4e0d\u5982\u91cd\u65b0\u5b66\u4e00\u904d\u3002<br \/>\n\u6bd4\u8d77XXblog\u4e0a\u7684\u6559\u7a0b\uff0c\u6211\u6700\u559c\u6b22\u7684\u8fd8\u662f\u8fd9\u4e2a\u7248\u672c\uff1a<a href=\"http:\/\/www.ics.uci.edu\/~eppstein\/161\/960229.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/www.ics.uci.edu\/~eppstein\/161\/960229.html<\/a><br \/>\n\u91cc\u9762\u7684\u4ee3\u7801\u5e72\u51c0\u5229\u843d\uff0c\u89e3\u91ca\u4e5f\u975e\u5e38\u7684\u6e05\u6670\uff0c\u4e00\u6b65\u4e00\u6b65\u600e\u4e48\u6765\u7684\u90fd\u5f88\u597d\u3002<br \/>\n\u8fd9\u4e2a\u95ee\u9898\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u7684\u95ee\u9898\uff0c\u6700\u57fa\u672c\u7684\u6837\u5b50\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre class=\"lang:default decode:true \">\u5047\u8bbe\u5f53\u524d\u641c\u7d22\u5230\u5b57\u7b26\u4e32A,B\u4e2d\u7684\u5b57\u7b26a[i],b[j].\nLCS[i,j]\u8868\u793a\u5b57\u4e32A,B\u5206\u522b\u5728\u4f4d\u7f6e[i][j]\u53ca\u4ee5\u540e\u7684\u6700\u5927\u5b50\u5e8f\u5217\u6570\u91cf\nIF A[i] == B[j]:\n    LCS[i,j] = 1 + LCS[i+1][j+1] # \u5f53\u524d\u7684\u7b97\u5165\uff0c\u7136\u540eA\uff0cB\u518d\u5f80\u540e\u632a\u4e00\u683c\nELSE\n    LCS[i,j] = max(LCS[i][j+1], LCS[i+1][j]) # \u5206\u522b\u632a\u52a8A,B\u540e\u4e00\u683c\u770b\u770b\u54ea\u4e2a\u597d\nENDIF<\/pre>\n<p>\u7531\u4e8e\u8fd9\u6837\u9012\u5f52\u80fd\u5f04\u51fa\u7684\u5b50\u95ee\u9898\u91cc\u9762\uff0c\u597d\u591a\u662f\u91cd\u590d\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u50cf\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90a3\u6837\uff0c\u91c7\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u7528\u975e\u9012\u5f52\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u5373\u4ece\u5b57\u7b26\u7684\u672b\u5c3e\u5f80\u524d\u63a8\u5bfcLCS[i][j]\uff0c\u8fd9\u6837\u5f53\u8ba1\u7b97LCS[i][j]\u7684\u65f6\u5019, LCS[i+1][j]\u6216LCS[i][j+1]\u5df2\u7ecf\u8ba1\u7b97\u597d\u4e86\u3002<br \/>\n\u5177\u4f53\u8fd8\u662f\u770b\u82f1\u6587\u6559\u7a0b\u5427\uff0c\u8fd9\u91cc\u7ed9\u51faC++\u5b9e\u73b0\u7684\u4ee3\u7801\u3002\u56e0\u4e3a\u4e0b\u5348\u4f8b\u4f1a\u8fd8\u5f97\u8bb2PPT\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u7528\u4e86\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u8ba1\u7b97\u597d\u7684\u5e8f\u5217\uff0c\u6709\u70b9\u4e11\u3002<\/p>\n<pre class=\"lang:c++ decode:true \">#include &lt;iostream&gt;\nusing namespace std;\n\/**\n * Longest Common Subsequence\n *\/\nint** storeMat;\nint max(int x, int y){\n    return x&gt;y? x:y;\n}\nint getLCSLength(char* A, char* B){\n    int sz_A = strlen(A);\n    int sz_B = strlen(B);\n    storeMat = new int*[sz_A+1];\n    for(int i=0; i&lt;=sz_A; i++){\n        storeMat[i] = new int[sz_B+1];  \/\/ Allocate memory\n    }\n    for(int i=sz_A; i&gt;=0; i--){\n        for(int j=sz_B; j&gt;=0; j--){\n            if(A[i] == '\\0' || B[j] == '\\0'){\n                storeMat[i][j] = 0;\n            } else if (A[i] == B[j]) {\n                storeMat[i][j] = 1 + storeMat[1+i][1+j];\n            } else {\n                storeMat[i][j] = max(storeMat[1+i][j], storeMat[i][1+j]);\n            }\n        }\n    }\n    return storeMat[0][0];\n}\nstring getLCS(char* A, char* B){\n    if(NULL == storeMat){\n        getLCSLength(A, B);\n    }\n    int i=0, j=0;\n    string result;\n    int sz_A = strlen(A); int sz_B = strlen(B);\n    while(i &lt; sz_A &amp;&amp; j&lt;sz_B){\n        if(A[i] == B[j]){\n            result += A[i];\n            i++;\n            j++;\n        } else if (storeMat[i+1][j] &gt; storeMat[i][j+1]){\n            i++;\n        } else {\n            j++;\n        }\n    }\n    return result;\n}\nint main() {\n    char a[50], b[50];\n    scanf(\"%s %s\", a, b);\n    cout &lt;&lt; getLCS(a, b);\n    return 0;\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e2a\u5f88\u8001\u7684\u95ee\u9898\u4e86\uff0c\u6700\u8fd1\u603b\u662f\u78b0\u5230\uff0c\u5927\u4e09\u65f6\u5019\u5b66\u7684\u7b97\u6cd5\u597d\u591a\u90fd\u5fd8\u8bb0\u4e86\uff0c\u4e0d\u5982\u91cd\u65b0\u5b66\u4e00\u904d\u3002 \u6bd4\u8d77XXblog\u4e0a\u7684\u6559\u7a0b\uff0c\u6211\u6700\u559c\u6b22\u7684\u8fd8\u662f\u8fd9\u4e2a\u7248\u672c\uff1ahttp:\/\/www.ics.uci.edu\/~eppstein\/161\/960229.html \u91cc\u9762\u7684\u4ee3\u7801\u5e72\u51c0\u5229\u843d\uff0c\u89e3\u91ca\u4e5f\u975e\u5e38\u7684\u6e05\u6670\uff0c\u4e00\u6b65\u4e00\u6b65\u600e\u4e48\u6765\u7684\u90fd\u5f88\u597d\u3002 \u8fd9\u4e2a\u95ee\u9898\u662f\u4e2a\u52a8\u6001\u89c4\u5212\u7684\u95ee\u9898\uff0c\u6700\u57fa\u672c\u7684\u6837\u5b50\u662f\u8fd9\u6837\u7684\uff1a \u5047\u8bbe\u5f53\u524d\u641c\u7d22\u5230\u5b57\u7b26\u4e32A,B\u4e2d\u7684\u5b57\u7b26a[i],b[j]. LCS[i,j]\u8868\u793a\u5b57\u4e32A,B\u5206\u522b\u5728\u4f4d\u7f6e[i][j]\u53ca\u4ee5\u540e\u7684\u6700\u5927\u5b50\u5e8f\u5217\u6570\u91cf IF A[i] == B[j]: LCS[i,j] = 1 + LCS[i+1][j+1] # \u5f53\u524d\u7684\u7b97\u5165\uff0c\u7136\u540eA\uff0cB\u518d\u5f80\u540e\u632a\u4e00\u683c ELSE LCS[i,j] = max(LCS[i][j+1], LCS[i+1][j]) # \u5206\u522b\u632a\u52a8A,B\u540e\u4e00\u683c\u770b\u770b\u54ea\u4e2a\u597d ENDIF \u7531\u4e8e\u8fd9\u6837\u9012\u5f52\u80fd\u5f04\u51fa\u7684\u5b50\u95ee\u9898\u91cc\u9762\uff0c\u597d\u591a\u662f\u91cd\u590d\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u50cf\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u90a3\u6837\uff0c\u91c7\u7528\u81ea\u5e95\u5411\u4e0a\u7684\u65b9\u6cd5\uff0c\u7528\u975e\u9012\u5f52\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002\u5373\u4ece\u5b57\u7b26\u7684\u672b\u5c3e\u5f80\u524d\u63a8\u5bfcLCS[i][j]\uff0c\u8fd9\u6837\u5f53\u8ba1\u7b97LCS[i][j]\u7684\u65f6\u5019, LCS[i+1][j]\u6216LCS[i][j+1]\u5df2\u7ecf\u8ba1\u7b97\u597d\u4e86\u3002 \u5177\u4f53\u8fd8\u662f\u770b\u82f1\u6587\u6559\u7a0b\u5427\uff0c\u8fd9\u91cc\u7ed9\u51faC++\u5b9e\u73b0\u7684\u4ee3\u7801\u3002\u56e0\u4e3a\u4e0b\u5348\u4f8b\u4f1a\u8fd8\u5f97\u8bb2PPT\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u7528\u4e86\u5168\u5c40\u53d8\u91cf\u4f20\u9012\u8ba1\u7b97\u597d\u7684\u5e8f\u5217\uff0c\u6709\u70b9\u4e11\u3002 #include &lt;iostream&gt; using namespace std; \/** * Longest Common Subsequence *\/ int** storeMat; int max(int x, int y){ return x&gt;y? x:y; } int getLCSLength(char* A, char* B){ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[63,66,141],"class_list":["post-1950","post","type-post","status-publish","format-standard","hentry","category-study","tag-lcs","tag-leetcode-oj","tag-141"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1950"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1950\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}