{"id":9021,"date":"2024-05-03T12:51:27","date_gmt":"2024-05-03T04:51:27","guid":{"rendered":""},"modified":"2024-05-03T12:51:27","modified_gmt":"2024-05-03T04:51:27","slug":"\u6d1b\u8c37P1439 \u3010\u6a21\u677f\u3011\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9021.html","title":{"rendered":"\u6d1b\u8c37P1439 \u3010\u6a21\u677f\u3011\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217"},"content":{"rendered":"

\"\u6d1b\u8c37P1439<\/p>\n

 1\u3001\u6734\u7d20\u505a\u6cd5<\/h2>\n

\u6734\u7d20\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217(LCS)\u505a\u6cd5\u5229\u7528dp\u6c42\u89e3\uff0c\u5b9a\u4e49dp[i][j]\u4e3a\u53ea\u53d6\u5e8f\u5217a\u524di\u4e2a\u6570\u5b57\u4e0e\u5e8f\u5217b\u524dj\u4e2a\u6570\u5b57\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002\u5bf9\u4e8edp[i][j]\u67094\u4e2d\u60c5\u51b5\uff1a1.a[i]\u4e0eb[j]\u90fd\u5728LCS\u4e2d\u30022.a[i]\u5728LCS\u4e2d\uff0cb[j]\u4e0d\u5728LCS\u4e2d3.a[i]\u4e0d\u5728LCS\u4e2d\uff0cb[j]\u4e0d\u5728LCS\u4e2d\u30024.a[i]\u4e0eB[j]\u90fd\u4e0d\u5728LCS\u4e2d\u3002<\/strong><\/p>\n

\u5982\u679ca[i]==b[j]\u5219dp[i][j]\u4e3a\u7b2c\u4e00\u79cd\u60c5\u51b5\uff0cdp[i][j]==d[i-1][j-1]+1;\u5982\u679ca[i]=b[j],\u5219\u4e3a\u540e\u4e09\u79cd\u60c5\u51b5\uff0c\u7b2c\u4e8c\u79cd\u60c5\u51b5dp[i][j]=dp[i][j-1],\u7b2c\u4e09\u79cd\u60c5\u51b5dp[i-1][j]\u3002\u7b2c\u56db\u79cd\u60c5\u51b5\u5305\u542b\u5728\u7b2c\u4e8c\u4e09\u79cd\u60c5\u51b5\u7684\u5e76\u96c6\u4e2d\u3002<\/strong><\/p>\n

\u6240\u4ee5\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u4e3a<\/strong><\/p>\n

\"\u6d1b\u8c37P1439<\/p>\n

#include<bits\/stdc++.h>\nusing namespace std;\nconst int N=1010;\nint dp[N][N],a[N],b[N];\nint main()\n{\n\tint n;\n\tcin>>n;\n    for(int i=1;i<=n;i++)\n    cin>>a[i];\n    for(int i=1;i<=n;i++)\n    cin>>b[i];\n    for(int i=1;i<=n;i++)\n    {\n    \tfor(int j=1;j<=n;j++)\n    \t{\n    \t\tif(a[i]==b[j])\n    \t\tdp[i][j]=dp[i-1][j-1]+1;\n    \t\telse\n    \t\tdp[i][j]=max(dp[i][j-1],dp[i-1][j]); \n    \t}\n    }\n    cout<<dp[n][n];\n}\n<\/code><\/pre>\n

 \u4f46\u8fd9\u9053\u9898\u7684n\u9ad8\u8fbe1e5\uff0c\u6734\u7d20\u505a\u6cd5\u65e0\u8bba\u65f6\u95f4\u8fd8\u662f\u6570\u7ec4\u90fd\u4e0d\u591f\u7528\uff0c\u4f46\u4e24\u5e8f\u5217\u957f\u5ea6\u76f8\u540c\u5e76\u4e14\u90fd\u662f1-n\u7684\u6392\u5217\uff0c\u56e0\u6b64\u53ef\u4ee5\u8fdb\u884c\u4f18\u5316\u5c06\u65f6\u95f4\u590d\u6742\u5ea6\u964d\u4e3anlogn\uff0c\u6570\u7ec4\u964d\u4e3a1\u7ef4\u3002<\/span><\/strong><\/p>\n

2.\u4f18\u5316\u505a\u6cd5<\/strong><\/h2>\n
#include<bits\/stdc++.h>\nusing namespace std;\nconst int N=1e5+10;\nint a[N],b[N],c[N],f[N];\nint len=1;\nint main()\n{\n\tint n;\n\tscanf(\"%d\",&n);\n\tfor(int i=1;i<=n;i++)\n\t{\n\tscanf(\"%d\",&a[i]);\n\tc[a[i]]=i;\n    }\n\tfor(int i=1;i<=n;i++)\n\t{\n\t\tscanf(\"%d\",&b[i]);\n\t\tb[i]=c[b[i]];\n\t}\n\tf[1]=b[1];\n\tfor(int i=2;i<=n;i++)\n\t{\n\t\tif(f[len]<b[i])\n\t\tf[++len]=b[i];\n\t\telse\n\t\tf[lower_bound(f+1,f+len+1,b[i])-f]=b[i];\n\t}\n\tprintf(\"%d\",len);\n}<\/code><\/pre>\n

\u5229\u7528f[i]\u5b58\u957f\u5ea6\u4e3ai\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u3002<\/strong><\/p>\n

\u628a\u5e8f\u5217\u4e8c\u6620\u5c04\u4e3ab[i]\u5728a\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6bd4\u5982a\u4e3a2 1 3 5 4,b\u4e3a3 2 1 4 5,\u5219b[1]\u6539\u4e3a3\uff0c\u5373b[1](3)\u5728a\u4e2d\u7684\u4f4d\u7f6e\u3002\u5e76\u4e14\u8bb0\u5f55\u6bcf\u4e00\u4e2a\u957f\u5ea6\u7684\u516c\u5171\u5b50\u5e8f\u5217\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5728a\u4e2d\u7684\u4f4d\u7f6e\uff0c\u56e0\u4e3a\u6211\u4eec\u4ece\u5de6\u5230\u53f3\u904d\u5386b\u6570\u7ec4\uff0c\u5982\u679cb\u4e2d\u5b58\u7684\u5f53\u524d\u5143\u7d20\u5728a\u7684\u4f4d\u7f6e\u6bd4\u5f53\u524d\u4fdd\u5b58\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u8fd8\u8981\u9760\u540e\uff0c\u5219\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6+1\uff0c\u5982\u679c\u4e0d\u662f\u7684\u8bdd\uff0c\u80af\u5b9a\u6709\u67d0\u4e00\u957f\u5ea6\u7684\u516c\u5171\u5b50\u5e8f\u5217\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f4d\u7f6e\u6bd4\u904d\u5386\u7684\u4f4d\u7f6e\u5927\uff0c\u5219\u7528\u5f53\u524d\u904d\u5386\u7684\u4f4d\u7f6e\u6765\u4ee3\u66ff\u8fd9\u4e00\u957f\u5ea6\u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f4d\u7f6e\uff0c\u8fd9\u662f\u4e00\u79cd\u8d2a\u5fc3\u505a\u6cd5\u3002<\/strong><\/p>\n

\"\u6d1b\u8c37P1439<\/p>\n

 <\/p>\n

 \u56e0\u4e3a\u662f\u4eceb\u6570\u7ec4\u4ece\u5de6\u5f80\u53f3\u904d\u5386\uff0c\u6240\u4ee5\u540e\u9762\u904d\u5386\u7684\u4f4d\u7f6e\u4e00\u5b9a\u5728b\u6570\u7ec4\u662f\u9760\u540e\u7684\uff0c\u800c\u4e14b\u6620\u5c04\u7684\u662fa\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5982\u56fe\u904d\u5386\u52303\u7684\u65f6\u5019f[2]=4\uff0c\u904d\u5386\u52302\u7684\u65f6\u5019\u53d1\u73b02\u5728a\u4e2d\u7684\u4f4d\u7f6e\u66f4\u9760\u524d\uff0c\u6240\u4ee5\u66f4\u65b0f[2]=2,\u518d\u904d\u5386\u52304\u7684\u65f6\u5019\u66f4\u65b0f[3]=3\uff0c\u5982\u679c\u4e0d\u628af[2]\u66f4\u65b0\u4e3a\u66f4\u9760\u524d\u7684\u4f4d\u7f6e\uff0c\u90a3\u4e48\u5219\u4e0d\u80fd\u628af[3]\u66f4\u65b0\u4e3a3\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"\u6d1b\u8c37P1439 \u3010\u6a21\u677f\u3011\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4f46\u8fd9\u9053\u9898\u7684n\u9ad8\u8fbe1e5\uff0c\u6734\u7d20\u505a\u6cd5\u65e0\u8bba\u65f6\u95f4\u8fd8\u662f\u6570\u7ec4\u90fd\u4e0d\u591f\u7528\uff0c\u4f46\u4e24\u5e8f\u5217\u957f\u5ea6\u76f8\u540c\u5e76\u4e14\u90fd\u662f1-n\u7684\u6392...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9021"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=9021"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9021\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=9021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=9021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=9021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}