AcWing 897. 最长公共子序列---Java
原题链接
简单
作者:
夏日的小提琴
,
2021-11-26 10:08:28
,
所有人可见
,
阅读 208
import java.io.*;
public class Main {
static int n, m;
static int[][] f;
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
String[] str = reader.readLine().split(" ");
n = Integer.parseInt(str[0]);
m = Integer.parseInt(str[1]);
f = new int[n + 1][m + 1];
char[] a = reader.readLine().toCharArray();
char[] b = reader.readLine().toCharArray();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1]) {
f[i][j] = f[i - 1][j - 1] + 1;
} else {
f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]);
}
}
}
writer.write(f[n][m] + "");
writer.flush();
writer.close();
reader.close();
}
}