最长公共子序列
给定两个长度分别为 N 和 M 的字符串 A 和 B,求既是 A 的子序列又是 B 的子序列的字符串长度最长是多少。
输入格式
第一行包含两个整数 N 和 M。
第二行包含一个长度为 N 的字符串,表示字符串 A。
第三行包含一个长度为 M 的字符串,表示字符串 B。
字符串均由小写字母构成。
输出格式
输出一个整数,表示最大长度。
数据范围
1≤N,M≤1000
输入样例:
4 5
acbd
abedc
输出样例:
3
算法:动态规划
分析
c++
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int f[N][N];
char a[N], b[N];
int main()
{
int n, m;
cin >> n >> m >> a + 1 >> b + 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(a[i] == b[j])
{
f[i][j] = max(f[i - 1][j - 1] + 1, f[i][j]);
}
}
}
cout << f[n][m] << endl;
return 0;
}
java
import java.util.*;
class Main
{
static final int N = 1010;
static int[][] f = new int[N][N];
static char[] a = new char[N];
static char[] b = new char[N];
static int n, m;
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
System.arraycopy(in.next().toCharArray(), 0, a, 1, n);
System.arraycopy(in.next().toCharArray(), 0, b, 1, m);
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= m; j++)
{
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
if(a[i] == b[j]) f[i][j] = Math.max(f[i][j], f[i - 1][j - 1] + 1);
}
}
System.out.println(f[n][m]);
}
}