{"id":1118,"date":"2024-02-18T20:21:09","date_gmt":"2024-02-18T12:21:09","guid":{"rendered":"http:\/\/ggapa.net:81\/?p=1118"},"modified":"2024-02-18T20:21:09","modified_gmt":"2024-02-18T12:21:09","slug":"lcs","status":"publish","type":"post","link":"http:\/\/ggapa.net:81\/2024\/02\/18\/lcs\/","title":{"rendered":"LCS"},"content":{"rendered":"
LCS\u2014\u2014\u7ecf\u5178\u7684 DP \u95ee\u9898\uff0c\u7ed9\u5b9a\u4e24\u4e2a\u957f\u5ea6\u4e3a $n$ \u7684\u6392\u5217\uff0c\u8bd5\u95ee\u4e8c\u8005\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002\u8fd9\u662f\u7ecf\u5178\u7684\u533a\u95f4 DP \u95ee\u9898\uff0c<\/p>\n
\u9996\u5148\u8003\u8651\u6734\u7d20\u505a\u6cd5\uff0c\u5b9a\u4e49 $dp[i][j]$ \u6765\u8868\u793a\u7b2c\u4e00\u4e2a\u4e32\u7684\u524d $i$ \u4f4d\uff0c\u7b2c\u4e8c\u4e2a\u4e32\u7684\u524d $j$ \u7684 $LCS$ \u957f\u5ea6\uff0c\u6613\u5f97\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\uff1a<\/p>\n
\u5982\u679c\u4e24\u4e2a\u5e8f\u5217\u6ca1\u6709\u65b0\u7684\u76f8\u540c\u5143\u7d20<\/p>\n
$dp[i][j] = \\max(dp[i-1][j],dp[i][j-1])$<\/p>\n<\/li>\n
\u5426\u5219<\/p>\n
$dp[i][j]= \\max(dp[i][j],dp[i-1][j-1]+1)$<\/p>\n
\u65f6\u95f4\u590d\u6742\u5ea6 $O(n^2)$ \u3002<\/p>\n<\/li>\n<\/ul>\n
#include <iostream>\nusing namespace std;\nconst int maxn = 1e4 + 5;\n\nint a[maxn], b[maxn], f[maxn][maxn];\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int n;\n cin >> n;\n for(int i = 1; i <= n; i++) cin >> a[i];\n for(int i = 1; i <= n; i++) cin >> b[i];\n for(int i = 1; i <= n; i++) {\n for(int j = 1; j <= n; j++) {\n f[i][j] = max(f[i-1][j], f[i][j-1]);\n if(a[i] == b[j]) f[i][j] = max(f[i][j], f[i-1][j-1] + 1);\n }\n }\n cout << f[n][n] << "\\n";\n return 0;\n}<\/code><\/pre>\n\u7531\u4e8e\u65f6\u95f4\u590d\u6742\u5ea6\u4e2d\u60e8\u4e86\u4e00\u4efd\u5e73\u65b9\uff0c\u6240\u4ee5\u8bf4\u5728\u5927\u591a\u6570\u9898\u76ee\u4e0b\u90fd\u4e0d\u9002\u7528\uff0c\u6b64\u65f6\u8003\u8651\u8fdb\u884c\u4f18\u5316\u3002\u8bd5\u7740\u5c06 $LCS$ \u95ee\u9898\u8f6c\u6362\u6210 $LIS$ \u95ee\u9898\uff0c\u56e0\u4e3a\u6211\u4eec\u4e24\u4e2a\u6392\u5217\uff0c\u800c\u6392\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20\u5747\u4e0d\u76f8\u540c\uff0c\u56e0\u6b64\u53ef\u4ee5\u6839\u636e\u5176\u51fa\u73b0\u7684\u4f4d\u7f6e\uff0c\u628a\u5143\u7d20\u91cd\u65b0\u7f16\u53f7\u3002<\/p>\n
\u89e3\u91ca\uff1a<\/p>\n
\n\u56e0\u4e3a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f\u6309\u4f4d\u5411\u540e\u6bd4\u5bf9\u7684\uff0c\u6240\u4ee5a\u5e8f\u5217\u6bcf\u4e2a\u5143\u7d20\u5728b\u5e8f\u5217\u4e2d\u7684\u4f4d\u7f6e\u5982\u679c\u9012\u589e\uff0c\u5c31\u8bf4\u660eb\u4e2d\u7684\u8fd9\u4e2a\u6570\u5728a\u4e2d\u7684\u8fd9\u4e2a\u6570\u6574\u4f53\u4f4d\u7f6e\u504f\u540e<\/strong>\u3002<\/p>\n\u2014\u2014 \u79bb\u6563\u5c0f\u6ce2\u53d8\u6362<\/p>\n<\/blockquote>\n
\u65f6\u95f4\u590d\u6742\u5ea6 $O(n \\log n)$\u3002<\/p>\n
#include <bits\/stdc++.h>\n#define rep(i, a, b) for (int i = (a), stOwxc = (b); i <= stOwxc; i++)\n#define per(i, a, b) for (int i = (a), stOwxc = (b); i >= stOwxc; i--)\nusing namespace std;\ntypedef long long ll;\n\nsigned main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n int n, x;\n cin >> n;\n unordered_map<int, int> mp;\n rep(i, 1, n) cin >> x, mp[x] = i;\n vector<int> L;\n rep(i, 1, n) {\n cin >> x;\n if (!mp.count(x))\n continue;\n int s = mp[x];\n auto it = lower_bound(L.begin(), L.end(), s);\n if (it == L.end())\n L.push_back(s);\n else\n *it = s;\n }\n cout << L.size() << '\\n';\n return 0;\n}<\/code><\/pre>\n\u6ce8\u610f\uff1a<\/p>\n
\nmap<\/code> \u7684\u5e38\u6570\u7a0d\u5927\uff0c\u53ef\u4ee5\u8003\u8651 unordered_map<\/code> \u6765\u89e3\u51b3\u95ee\u9898\uff0c\u5728\u6d1b\u8c37\u6a21\u677f\u9898\u4e2d\uff0c\u540e\u8005\u6bd4\u524d\u8005\u5feb\u4e86 $50\\%$ \u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"LCS\u2014\u2014\u7ecf\u5178\u7684 DP \u95ee\u9898\uff0c\u7ed9\u5b9a\u4e24\u4e2a\u957f\u5ea6\u4e3a $n$ \u7684\u6392\u5217\uff0c\u8bd5\u95ee\u4e8c\u8005\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002\u8fd9\u662f\u7ecf\u5178\u7684\u533a\u95f4 DP […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[31],"_links":{"self":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1118"}],"collection":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/comments?post=1118"}],"version-history":[{"count":1,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1118\/revisions"}],"predecessor-version":[{"id":1119,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1118\/revisions\/1119"}],"wp:attachment":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/media?parent=1118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/categories?post=1118"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/tags?post=1118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}