{"id":8792,"date":"2024-05-27T08:01:01","date_gmt":"2024-05-27T00:01:01","guid":{"rendered":""},"modified":"2024-05-27T08:01:01","modified_gmt":"2024-05-27T00:01:01","slug":"\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217c++\u4ee3\u7801_\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217 \u52a8\u6001\u89c4\u5212\u56fe\u89e3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8792.html","title":{"rendered":"\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217c++\u4ee3\u7801_\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217 \u52a8\u6001\u89c4\u5212\u56fe\u89e3"},"content":{"rendered":"
\u4e00\u3001\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u4e0e\u5206\u6cbb\u6cd5\u7684\u5f02\u540c<\/span><\/strong><\/p>\n \u76f8\u540c\u70b9<\/span>\uff1a<\/p>\n A\u3001\u4e8c\u8005\u5747\u662f\u5c06\u5f85\u6c42\u89e3\u7684\u95ee\u9898\u5206\u6210\u82e5\u5e72\u5b50\u95ee\u9898<\/span>\u6765\u6c42\u89e3\u3002 <\/p>\n B\u3001\u4e8c\u8005\u5728\u7f16\u5199\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u90fd\u8981\u7528\u5230\u9012\u5f52<\/span>\u3002<\/p>\n \u4e0d\u540c\u70b9<\/span>\uff1a<\/p>\n A\u3001\u5206\u6cbb\u6cd5\u6c42\u89e3\u7684\u95ee\u9898\uff0c\u5728\u5c06\u95ee\u9898\u5206\u6210\u82e5\u5e72\u5b50\u95ee\u9898\u4e4b\u540e\uff0c\u5176\u5b50\u95ee\u9898\u4e4b\u95f4\u662f\u72ec\u7acb\u5b58\u5728<\/span>\u7684\uff0c\u6ca1\u6709\u76f8\u4e92\u5173\u8054<\/span>\u3002\u800c\u52a8\u6001\u89c4\u5212\u95ee\u9898\u5212\u5206\u540e\u5f97\u5230\u7684\u5b50\u95ee\u9898\u4e4b\u95f4\u76f8\u4e92\u5173\u8054\u3002<\/p>\n B\u3001\u52a8\u6001\u89c4\u5212\u95ee\u9898\u5728\u6c42\u89e3\u65f6\u9700\u8981\u4e00\u4e2a\u8868<\/span>\u6765\u8bb0\u5f55\u5df2\u89e3\u51b3\u7684\u5b50\u95ee\u9898\u7684\u7b54\u6848\uff0c\u4ece\u800c\u907f\u514d\u5927\u91cf\u7684\u91cd\u590d\u8ba1\u7b97\u3002\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u9002\u5408\u7528\u4e8e\u89e3\u6700\u4f18\u5316\u95ee\u9898\u3002\u8fd9\u5c31\u8868\u660e\u8fd9\u7c7b\u6700\u4f18\u5316\u95ee\u9898\u53ef\u80fd\u6709\u5f88\u591a\u89e3\uff0c\u4f46\u4e0d\u4e00\u5b9a\u90fd\u662f\u6700\u4f18\u89e3\uff0c\u800c\u5229\u7528\u672c\u7b97\u6cd5\u627e\u51fa\u7684\u89e3\u53ef\u80fd\u8fd8\u662f\u4f17\u591a\u6700\u4f18\u89e3\u91cc\u7684\u4e00\u4e2a<\/span><\/p>\n \u4e8c\u3001\u6700\u4f18\u89e3\u95ee\u9898\u7ed3\u6784\u6027\u8d28<\/span><\/strong><\/p>\n \u6700\u4f18\u5b50\u7ed3\u6784\uff1a\u5f53\u95ee\u9898\u7684\u6700\u4f18\u89e3\u5305\u542b\u4e86\u5176\u5b50\u95ee\u9898\u7684\u6700\u4f18\u89e3\u65f6\uff0c\u79f0\u8be5\u95ee\u9898\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\u3002<\/span><\/p>\n \u91cd\u53e0\u5b50\u95ee\u9898\uff1a\u5728\u7528\u9012\u5f52\u7b97\u6cd5\u81ea\u9876\u5411\u4e0b\u89e3\u51b3\u95ee\u9898\u7684\u65f6\u5019\uff0c\u6bcf\u6b21\u4ea7\u751f\u7684\u5b50\u95ee\u9898\u5e76\u4e0d\u603b\u662f\u65b0\u95ee\u9898\uff0c\u6709\u4e9b\u5b50\u95ee\u9898\u88ab\u53cd\u590d\u8ba1\u7b97\u3002\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u6b63\u5f0f\u5229\u7528\u8fd9\u79cd\u5b50\u95ee\u9898\u7684\u91cd\u53e0\u6027\u8d28\uff0c\u5bf9\u6bcf\u4e2a\u5b50\u95ee\u9898\u53ea\u89e3\u4e00\u6b21\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u8868\u683c\u4e2d\u3002<\/span><\/p>\n \u4ece\u4e00\u822c\u610f\u4e49\u4e0a\u6765\u8bb2\uff0c\u95ee\u9898\u6240\u5177\u6709\u7684\u8fd9\u4e24\u4e2a\u91cd\u8981\u6027\u8d28\u662f\u8be5\u95ee\u9898\u53ef\u7528\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u6c42\u89e3\u7684\u57fa\u672c\u8981\u7d20\u3002<\/span><\/p>\n \u4e09\u3001\u52a8\u6001\u89c4\u5212\u95ee\u9898\u5206\u6790\u53ca\u89e3\u51b3\u6b65\u9aa4<\/span><\/strong><\/p>\n A\u3001\u627e\u51fa\u6700\u4f18\u89e3\u7684\u6027\u8d28\uff0c\u523b\u753b\u5176\u7ed3\u6784\u7279\u5f81\u3002<\/p>\n B\u3001\u9012\u5f52\u5b9a\u4e49\u6700\u4f18\u503c<\/p>\n C\u3001\u81ea\u5e95\u5411\u4e0a\u8ba1\u7b97\u6700\u4f18\u503c\uff08\u6700\u4f18\u503c\u53ea\u662f\u4e00\u4e2a\u7ed3\u679c\uff0c\u6700\u4f18\u89e3\u662f\u4e00\u4e2a\u8fc7\u7a0b\uff09<\/span><\/p>\n D\u3001\u6839\u636e\u5f97\u5230\u7684\u4fe1\u606f\u6784\u9020\u6700\u4f18\u89e3\uff08A-C\u662f\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u7684\u8fdb\u672c\u6b65\u9aa4\uff0c\u6b64\u6b65\u5728\u9700\u8981\u6c42\u51fa\u95ee\u9898\u7684\u6700\u4f18\u89e3\u65f6\u624d\u9700\u6267\u884c\uff09<\/span><\/p>\n \u56db\u3001\u95ee\u9898\u63cf\u8ff0\uff08\u95ee\u9898\u5927\u610f\uff09<\/strong><\/span><\/p>\n \u5b50\u5e8f\u5217\uff1a\u4e00\u4e2a\u7ed9\u5b9a\u5e8f\u5217\u4e2d\uff0c\u5220\u9664\u82e5\u5e72\u5143\u7d20\u540e\u5f97\u5230\u7684\u5e8f\u5217\uff0c\u5c31\u662f\u8be5\u5e8f\u5217\u7684\u5b50\u5e8f\u5217\u3002<\/p>\n \u516c\u5171\u5b50\u5e8f\u5217\uff1a\u5982\u679c\u6709\u4e24\u4e2a\u5e8f\u5217\u6309\u7167\u4e0a\u8ff0\u6b65\u9aa4\u4e4b\u540e\u5f97\u5230\u7684\u5b50\u5e8f\u5217\u76f8\u540c\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b50\u5e8f\u5217\u88ab\u79f0\u4e3a\u8fd9\u4e24\u4e2a\u5e8f\u5217\u7684\u516c\u5171\u5b50\u5e8f\u5217\u3002<\/p>\n \u4f8b\uff1a\u6709\u5982\u4e0b\u4e24\u4e2a\u5e8f\u5217<\/p>\n X={A\u3001B\u3001C\u3001B\u3001D\u3001A\u3001B}\uff0cY={B\u3001D\u3001C\u3001A\u3001B\u3001A}<\/p>\n \u5219\u5e8f\u5217Z={B\u3001C\u3001A}\u5c31\u662f\u4e8c\u8005\u7684\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u90a3\u4e48\u4ec0\u4e48\u662f\u6700\u957f\u7684\u516c\u5171\u5b50\u5e8f\u5217<\/span>\uff0c\u5927\u5bb6\u5e94\u8be5\u77e5\u9053\u4e86\u5427\uff1f<\/p>\n \u5728\u8fd9\u91cc\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1a\u4e00\u4e2a\u957f\u5ea6\u4e3an\u7684\u5e8f\u5217\uff0c\u5b83\u4e00\u5171\u67092\u7684n\u6b21\u65b9\u4e2a\u5b50\u5e8f\u5217\uff0c\u6709(2^n \u2013 1)\u4e2a\u975e\u7a7a\u5b50\u5e8f\u5217\u3002\u5927\u5bb6\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u8bf4\u7684\u662f\u5e8f\u5217\uff0c\u548c\u539f\u6765\u7684\u987a\u5e8f\u6709\u5173\u8054\uff0c\u800c\u4e0d\u662f\u9ad8\u4e2d\u6240\u5b66\u65e0\u5e8f\u7684\u96c6\u5408\u3002<\/span><\/p>\n \u4e0b\u9762\u5c06\u6309\u7167\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u8bbe\u8ba1\u7684\u6b65\u9aa4\u6765\u8bbe\u8ba1\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u7b97\u6cd5<\/p>\n 1\u3001\u95ee\u9898\u7ed3\u6784<\/p>\n <\/p>\n \u4e0a\u8ff0\u8bc1\u660e\u4e4b\u540e\uff0c\u672c\u95ee\u9898\u5177\u6709\u6700\u4f18\u5b50\u7ed3\u6784\u6027\u8d28\u3002<\/p>\n 2\u3001\u9012\u5f52\u7ed3\u6784<\/p>\n \u8981\u627e\u51faX={x1,x2,...,xm}\u548cY={y1,y2,...,yn}\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u53ef\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u9012\u5f52\uff1a<\/p>\n A\u3001\u5f53Xm=Yn\u65f6\uff0c\u627e\u51faXm-1\u548cYn-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u7136\u540e\u5728\u5176\u5c3e\u90e8\u52a0\u4e0aXm\uff08Xm=Yn\uff09\uff0c\u5373\u53ef\u5f97X\u3001Y\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002<\/span><\/p>\n B\u3001\u5f53Xm\uff01=Yn\u65f6\uff0c\u5219\u5f97\u89e3\u51b3\u4e24\u4e2a\u5b50\u95ee\u9898\uff1aXm-1\u548cY\u7684\u4e00\u4e2a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u53caX\u548cYn-1\u7684\u4e00\u4e2a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u4e8c\u8005\u4e2d\u8f83\u957f\u8005\u5373\u4e3aX\u548cY\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002<\/span><\/p>\n \u7531\u8fd9\u4e2a\u9012\u5f52\u7ed3\u6784\u6765\u770b\uff0c\u6b64\u95ee\u9898\u5177\u6709\u5b50\u95ee\u9898\u91cd\u53e0\u6027\u8d28\uff0c\u4f8b\u5982\uff1a\u5728\u8ba1\u7b97X\u548cY\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u65f6\uff0c\u53ef\u80fd\u8981\u8ba1\u7b97X\u548cYn-1\u53caXm-1\u548cY\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898\u90fd\u5305\u542b\u4e00\u4e2a\u516c\u5171\u5b50\u95ee\u9898\uff0c\u5373\u8981\u8ba1\u7b97Xm-1\u548cYn-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002<\/span><\/p>\n \u9996\u5148\u5efa\u7acb\u5b50\u95ee\u9898\u6700\u4f18\u503c\u7684\u9012\u5f52\u5173\u7cfb\uff0c\u7528\u4ecec[i][j]\u8bb0\u5f55\u5e8f\u5217Xi\u548cYi\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u3002\u5176\u4e2d\uff0cXi={x1,x2,...,xi};Yj={y1,y2,...,yj}<\/p>\n \u5f53i=0\u6216j=0\u65f6\uff0c\u7a7a\u5e8f\u5217\u662fXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\uff0c\u6240\u4ee5\u6b64\u65f6c[i][j]=0;<\/span><\/p>\n \u5176\u4ed6\u60c5\u51b5\uff1a<\/span><\/p>\n A\u3001i>0;j=0\u65f6,c[i][j]=0;<\/span><\/p>\n B\u3001i,j>0;Xi=Yj\u65f6\uff0cc[i][j]=c[i-1][j-1]+1<\/span><\/p>\n C\u3001i,j>0;Xi!=Yj\u65f6\uff0cc[i][j]=max{c[i][j-1],c[i-1][j]}<\/span><\/p>\n 3\u3001\u8ba1\u7b97\u6700\u4f18\u503c<\/p>\n \u76f4\u63a5\u5229\u7528\u4e0a\u8ff0\u9012\u5f52\u5f0f\uff0c\u5199\u51fa\u4ee3\u7801\u3002\u5176\u4e2dX\uff0cY\u662f\u8f93\u5165\u7684\u4e24\u4e2a\u5e8f\u5217\uff0c \uff0cb[i][j]\u8bb0\u5f55c[i][j]\u7684\u503c\u662f\u7531\u54ea\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u7684\uff0c\u8fd9\u5728\u6784\u9020\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u65f6\u8981\u7528\u5230\u3002<\/p>\n 4\u3001\u6784\u9020\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217<\/p>\n \u7528\u4e8c\u7ef4\u6570\u7ec4b\u5feb\u901f\u6784\u9020\u5e8f\u5217X\u548cY\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002\u9996\u5148\u4eceb[m][n]\u5f00\u59cb\uff0c\u4f9d\u7167\u5176\u503c\u5728\u6570\u7ec4b\u4e2d\u641c\u7d22\u3002<\/p>\n A\u3001\u5f53\u5728b[i][j]=1\u65f6\uff0c\u8868\u793aXi\u548cYi\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f\u7531Xi-1\u548cYi-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u5728\u5c3e\u90e8\u52a0\u4e0aXi\u6240\u5f97\u5230\u7684\u3002<\/p>\n B\u3001\u5f53\u5728b[i][j]=2\u65f6\uff0c\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi-1\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002<\/p>\n C\u3001\u5f53\u5728b[i][j]=3\u65f6\uff0c\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi\u548cYj-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002<\/p>\n \u5982\u6b64\uff0c\u5c06\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u6253\u5370\u51fa\u6765\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \u6240\u4ee5\uff0c\u4e0a\u8ff0\u7684\u6240\u6709\u6b65\u9aa4\u8f6c\u53d8\u4e3a\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \u628ac[i][j]\u548cb[i][j]\u5bf9\u5e94\u8d77\u6765\u4e4b\u540e\uff0c\u5f97\u5230\u4e0b\u56fe\uff1a<\/p>\n \u4ece\u4e0b\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u7528\u4e8c\u7ef4\u6570\u7ec4b[i][j]\u4e2d\u7684\u4fe1\u606f\u53ef\u4ee5\u6784\u5efa\u51faX\u548cY\u7684\u4e00\u4e2a\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002\u4eceb[xn][yn]\u5f00\u59cb\uff0c\u6cbf\u7740\u6a58\u9ec4\u8272\u7684\u7bad\u5934\u65b9\u5411\u8ffd\u8e2a\uff1a<\/p>\n A\u3001b[i][j]\u7b49\u4e8e1\u65f6\uff0c\u5de6\u659c45\u00b0\u5411\u4e0a\u8df3\uff0c\u5e76\u4e14\u8d77\u8df3\u7684\u5b57\u6bcd\u5c31\u662fLCS\u7684\u4e00\u4e2a\u6210\u5458<\/span><\/p>\n B\u3001b[i][j]\u7b49\u4e8e2\u65f6\uff0c\u7ad6\u76f4\u5411\u4e0a\u8df3\u3002<\/span><\/p>\n C\u3001b[i][j]\u7b49\u4e8e3\u65f6\uff0c\u6c34\u5e73\u5411\u5de6\u8df3\u3002<\/span><\/p>\n <\/p>\n \u53c2\u8003\uff1a\u300a\u8ba1\u7b97\u673a\u7b97\u6cd5\u8bbe\u8ba1\u4e0e\u5206\u6790\u00b7\u7b2c\u4e94\u7248 \u300b\u738b\u6653\u4e1c\u7f16\u8457<\/p>\n","protected":false},"excerpt":{"rendered":"\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217c++\u4ee3\u7801_\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217 \u52a8\u6001\u89c4\u5212\u56fe\u89e3\u4e00\u3001\u52a8\u6001\u89c4\u5212\u7b97\u6cd5\u4e0e\u5206\u6cbb\u6cd5\u7684\u5f02\u540c\u76f8\u540c\u70b9\uff1aA\u3001\u4e8c\u8005\u5747\u662f\u5c06\u5f85\u6c42\u89e3\u7684\u95ee\u9898\u5206\u6210...","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\/8792"}],"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=8792"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8792\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8792"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8792"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8792"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\u597d\u4e86\uff0c\u6709\u4e86\u4e0a\u9762\u52a8\u6001\u89c4\u5212\u7684\u57fa\u7840\u77e5\u8bc6\u540e\uff0c\u6211\u4eec\u5f00\u59cb\u672c\u6587\u7684\u201c\u91cd\u5934\u620f\u201d\u2014\u2014\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u3002<\/h4>\n
void LCSLength(int m,int n,char x[],char y[],int c[][50],int b[][50])\n{\n int i,j;\n for(i=1;i<=m;i++)\/\/\u5f53j=0\u65f6\uff0cc[i][j]=0\n c[i][0]=0;\n for(j=1;j<=n;j++)\/\/\u5f53i=0\u65f6\uff0cc[i][j]=0\n c[0][j]=0;\n for(i=1;i<=m;i++)\/\/\u5bf9\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u8fdb\u884c\u8bb0\u5f55\n for(j=1;j<=n;j++)\n {\n if(x[i]==y[j])\/\/Xi=Yj\u7684\u60c5\u51b5\n {\n c[i][j]=c[i-1][j-1]+1;\/\/c[i][j]\u5b58\u50a8Xi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\n b[i][j]=1; \/\/\u659c\u5411\u4e0a \/\/b[i][j]\u8bb0\u5f55c[i][j]\u7684\u503c\u662f\u7531\u54ea\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u7684\n }\n else if(c[i-1][j]>=c[i][j-1])\n {\n c[i][j]=c[i-1][j];\n b[i][j]=2;\/\/\u7ad6\u76f4\u5411\u4e0a\n }\n else\n {\n c[i][j]=c[i][j-1];\n b[i][j]=3;\/\/\u6c34\u5e73\u5411\u5de6\n }\n }\n}<\/code><\/pre>\n
void LCS(int i,int j,char x[], int b[][50])\n{\n if(i==0||j==0)\n return;\n if(b[i][j]==1)\/\/\u8868\u793aXi\u548cYi\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f\u7531Xi-1\u548cYi-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u5728\u5c3e\u90e8\u52a0\u4e0aXi\u6240\u5f97\u5230\u7684\n {\/\/b[i][j]==1\u65f6\uff0c\u8868\u793a\u5de6\u659c45\u00b0\u5411\u4e0a\uff0c\u4e5f\u5373\u8fd9\u4e2a\u65f6\u5019\u5bf9\u5e94\u7684\u5b57\u6bcd\u65f6LCS\u4e2d\u7684\u4e00\u4e2a\u3002\n LCS(i-1,j-1,x,b);\n cout<<x[i]<<\" \";\n }\n else if(b[i][j]==2)\/\/\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi-1\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002\n LCS(i-1,j,x,b);\n else\/\/\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi\u548cYj-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002\n LCS(i,j-1,x,b);\n}<\/code><\/pre>\n
#include<stdlib.h>\n#include<stdio.h>\n#include<iostream>\nusing namespace std;\nint c[50][50];\nint b[50][50];\nchar x[1000];\/\/\u5b58\u653eX\u5b57\u7b26\u4e32\nchar y[1000];\/\/\u5b58\u653eY\u5b57\u7b26\u4e32\nvoid LCSLength(int m,int n,char x[],char y[],int c[][50],int b[][50])\n{\n int i,j;\n for(i=1;i<=m;i++)\/\/\u5f53j=0\u65f6\uff0cc[i][j]=0\n c[i][0]=0;\n for(j=1;j<=n;j++)\/\/\u5f53i=0\u65f6\uff0cc[i][j]=0\n c[0][j]=0;\n for(i=1;i<=m;i++)\/\/\u5bf9\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\u8fdb\u884c\u8bb0\u5f55\n for(j=1;j<=n;j++)\n {\n if(x[i]==y[j])\/\/Xi=Yj\u7684\u60c5\u51b5\n {\n c[i][j]=c[i-1][j-1]+1;\/\/c[i][j]\u5b58\u50a8Xi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u7684\u957f\u5ea6\n b[i][j]=1; \/\/\u659c\u5411\u4e0a \/\/b[i][j]\u8bb0\u5f55c[i][j]\u7684\u503c\u662f\u7531\u54ea\u4e2a\u5b50\u95ee\u9898\u7684\u89e3\u5f97\u6765\u7684\n }\n else if(c[i-1][j]>=c[i][j-1])\n {\n c[i][j]=c[i-1][j];\n b[i][j]=2;\/\/\u7ad6\u76f4\u5411\u4e0a\n }\n else\n {\n c[i][j]=c[i][j-1];\n b[i][j]=3;\/\/\u6c34\u5e73\u5411\u5de6\n }\n }\n}\nvoid LCS(int i,int j,char x[], int b[][50])\n{\n if(i==0||j==0)\n return;\n if(b[i][j]==1)\/\/\u8868\u793aXi\u548cYi\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u662f\u7531Xi-1\u548cYi-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u5728\u5c3e\u90e8\u52a0\u4e0aXi\u6240\u5f97\u5230\u7684\n {\/\/b[i][j]==1\u65f6\uff0c\u8868\u793a\u5de6\u659c45\u00b0\u5411\u4e0a\uff0c\u4e5f\u5373\u8fd9\u4e2a\u65f6\u5019\u5bf9\u5e94\u7684\u5b57\u6bcd\u65f6LCS\u4e2d\u7684\u4e00\u4e2a\u3002\n LCS(i-1,j-1,x,b);\n cout<<x[i]<<\" \";\n }\n else if(b[i][j]==2)\/\/\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi-1\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002\n LCS(i-1,j,x,b);\n else\/\/\u8868\u793aXi\u548cYj\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e0eXi\u548cYj-1\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u76f8\u540c\u3002\n LCS(i,j-1,x,b);\n}\nint main()\n{\n int xn,yn;\/\/XY\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\n cout<<\"\u8bf7\u8f93\u5165X\u96c6\u5408\u7684\u5143\u7d20\u4e2a\u6570\uff1a\"<<endl;\n cin>>xn;\n cout<<\"\u8bf7\u8f93\u5165X\u96c6\u5408\u7684\u5143\u7d20\uff1a\"<<endl;\n int i=0;\/\/\u7528\u4e8e\u5faa\u73af\u8f93\u5165X\u548cY\u7684\u5b57\u7b26\u4e32\n for(i=1;i<=xn;i++)\n cin>>x[i];\n cout<<\"\u8bf7\u8f93\u5165y\u96c6\u5408\u7684\u5143\u7d20\u4e2a\u6570\uff1a\"<<endl;\n cin>>yn;\n cout<<\"\u8bf7\u8f93\u5165y\u96c6\u5408\u7684\u5143\u7d20\uff1a\"<<endl;\n for(i=1;i<=yn;i++)\n cin>>y[i];\n LCSLength(xn,yn,x,y,c,b);\n\tcout << \"X\u548cY\u7684\u6700\u957f\u516c\u5171\u5b50\u5e8f\u5217\u4e3a\uff1a\";\n LCS(xn,yn,x,b);\n\tcout << endl;\n\t\n\tint j = 0;\n\tint m = 0;\n\tprintf(\"b[i][j]\u4e2d\u7684\u6570\u5b57\uff1a\\n\");\n\tfor (i = 0; i <=xn; i++)\n\t\tfor (j = 0; j <= yn; j++)\n\t\t{ \n\t\t\tprintf(\"%d \", b[i][j]);\n\t\t\tm++;\n\t\t\tif (m == xn)\n\t\t\t{\n\t\t\t\tm = 0;\n\t\t\t\tprintf(\"\\n\");\n\t\t\t}\n\t\t\t\t \n\t\t}\n\tm = 0;\n\tprintf(\"c[i][j]\u4e2d\u7684\u6570\u5b57\uff1a\\n\");\n\tfor (i = 0; i <= xn; i++)\n\t\tfor (j = 0; j <=yn; j++)\n\t\t{\n\t\t\tprintf(\"%d \", c[i][j]);\n\t\t\tm++;\n\t\t\tif (m == xn)\n\t\t\t{\n\t\t\t\tprintf(\"\\n\");\n\t\t\t\tm = 0;\n\n\t\t\t}\n\t\t\t\t\n\t\t}\t\n\tsystem(\"pause\");\n return 0;\n}\n<\/code><\/pre>\n