赞
踩
如果在这里获得过启发和思考,希望点赞支持!对于内容有不同的看法欢迎来信交流。
技术栈 >> java
邮箱 >> 15673219519@163.com
@Data
@AllArgsConstructor
public static class Row {
// 格子信息
private List<Cell> cells;
// diff + - =
private String diff;
// 行号
private Integer rowNum;
// 旧行号
private Integer oldRowNum;
}
private static List<Row> compareLines(List<Row> lines1, List<Row> lines2) { List<Row> differences = new ArrayList<>(); int[][] LCS = new int[lines1.size() + 1][lines2.size() + 1]; // 计算LCS矩阵 for (int i = 1; i <= lines1.size(); i++) { for (int j = 1; j <= lines2.size(); j++) { if (lines1.get(i - 1).equals(lines2.get(j - 1))) { LCS[i][j] = LCS[i - 1][j - 1] + 1; } else { LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]); } } } // 从LCS矩阵中提取差异 int i = lines1.size(); int j = lines2.size(); while (i > 0 || j > 0) { if (i > 0 && j > 0 && lines1.get(i - 1).equals(lines2.get(j - 1))) { Row row = lines2.get(j - 1); row.setDiff("="); row.setRowNum(j); row.setOldRowNum(i); differences.add(row); i--; j--; } else if (j > 0 && (i == 0 || LCS[i][j - 1] >= LCS[i - 1][j])) {// + Row row = lines2.get(j - 1); row.setDiff("+"); row.setRowNum(j); differences.add(row); j--; } else { // - Row row = lines1.get(i - 1); row.setDiff("-"); row.setRowNum(i); row.setOldRowNum(i); differences.add(row); i--; } } // 翻转差异列表 List<Row> reversed = new ArrayList<>(); for (int k = differences.size() - 1; k >= 0; k--) { reversed.add(differences.get(k)); } return reversed; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。