LCS
来源:互联网 发布:中煤集团网络党校 编辑:程序博客网 时间:2024/05/03 17:22
大神级写法借鉴一下
import java.util.Scanner;
public class LCS {
public static void main(String[] args) {
String x = new Scanner(System.in).nextLine();
String y = new Scanner(System.in).nextLine();
int M = x.length();
int N = y.length();
// opt[i][j] = length of LCS of x[i..M] and y[j..N]
int[][] opt = new int[M+1][N+1];
// compute length of LCS and all subproblems via dynamic programming
for (int i = M-1; i >= 0; i--) {
for (int j = N-1; j >= 0; j--) {
if (x.charAt(i) == y.charAt(j))
opt[i][j] = opt[i+1][j+1] + 1;
else
opt[i][j] = Math.max(opt[i+1][j], opt[i][j+1]);
}
}
// recover LCS itself and print it to standard output
int i = 0, j = 0;
while(i < M && j < N) {
if (x.charAt(i) == y.charAt(j)) {
System.out.print(x.charAt(i));
i++;
j++;
}
else if (opt[i+1][j] >= opt[i][j+1]) i++;
else j++;
}
System.out.println();
}
}
- LCS
- LCS
- lcs
- LCS
- LCS
- LCS
- LCS
- LCS
- LCS
- LCS
- lcs
- LCS
- LCS
- lcs
- LCS
- LCS
- LCS
- LCS
- Speed Limit
- C#之委托学习
- CoordinatorLayout-带图片伸缩工具栏
- 达内科技是干什么的
- Trade - HDU 3401 单调队列优化
- LCS
- 欢迎使用CSDN-markdown编辑器
- c#实现redis客户端(一)
- python环境变量的配置
- 如何获取最新的代码?
- [leetcode] 83.Remove Duplicates from Sorted List
- XML文档定义有几种形式?它们之间有何本质区别?解析XML文档有哪几种方式?
- 最小生成树
- AFNetWork获取非json格式网数据方法;