题解 | #最长公共子序列(一)#
最长公共子序列(一)
http://www.nowcoder.com/practice/672ab5e541c64e4b9d11f66011059498
#include<iostream>
using namespace std;
int graph[1001][1001];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
string a, b;
cin >> n >> m;
cin >> a >> b;
a = " " + a;
b = " " + b;
for (int i = 0; i <= m; ++i) {
for (int j = 0; j <= n; ++j) {
if (i == 0 || j == 0)
graph[i][j] = 0;
else {
if (b[i] == a[j])
graph[i][j] = max(graph[i - 1][j - 1] + 1, max(graph[i - 1][j],graph[i][j - 1]));
else
graph[i][j] = max(graph[i - 1][j], graph[i][j - 1]);
}
}
}
cout << graph[m][n];
}