Решил не останавливаться на условии и сделать более универсальный способ сравнения. Сначала строю матрицу сравнения, потом получаю графы и выбираю максимальный по длине. Однако, есть проблема с условием:
строка1 строка1 SAME строка1
строка2 REMOVED строка2
строка3 строка3 SAME строка3
строка4 REMOVED строка4
строка5 строка5 SAME строка5
строка0 ADDED строка0
строка1 строка1 SAME строка1
строка2 REMOVED строка2
строка3 строка3 SAME строка3
строка5 ADDED строка5
строка4 строка4 SAME строка4
строка5 REMOVED строка5
Граф по одинаковым точкам полностью аналогичен такому же с альтернативным окончанием
...
строка3 строка3 SAME строка3
строка4 REMOVED строка4
строка5 строка5 SAME строка5
строка4 ADDED строка4
и аналогично если поменять местами файлы.
А какой путь в данном случае правильный никак не оговаривается. Прочитал все темы и все приведенные примеры в них работают правильно.
В общем, мелкая глупость, а валидацию не проходит - ругается "Убедись, что список lines в нужных местах содержит операции ADDED c нужными строками.".
package com.javarush.task.task19.task1916;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/*
Отслеживаем изменения
*/
public class Solution {
public static List<LineItem> lines = new ArrayList<LineItem>();
public static List<Route> routes = new ArrayList<Route>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String f1 = br.readLine(), f2 = br.readLine();
br.close();
BufferedReader br1 = new BufferedReader (new FileReader (f1));
BufferedReader br2 = new BufferedReader (new FileReader (f2));
ArrayList<String> al1 = new ArrayList<String>(), al2 = new ArrayList<String>();
int max = 0;
while (br1.ready())
al1.add(br1.readLine());
while (br2.ready())
al2.add(br2.readLine());
br1.close();
br2.close();
int[][] m = new int[al1.size()][al2.size()];
for (int i = 0;i<al1.size();i++) {
for (int j = 0; j < al2.size(); j++) {
if (al1.get(i).equals(al2.get(j)))
m[i][j] = 1;
else
m[i][j] = 0;
}
}
for (int i = 0;i<al1.size();i++)
for (int j = 0;j<al2.size();j++)
{
if (m[i][j] == 1)
{
Route.addPoint (i,j);
}
}
Route route = Route.getMaxRoute();
int col = 0, p = 0;
for (int i = 0;i < al1.size();i++)
{
Point point = null;
if (p < route.points.size())
point = route.points.get(p);
else
point = route.points.get(route.points.size()-1);
if (point.getRow() != i)
//System.out.println("REMOVED " + al1.get(i));
lines.add(new LineItem(Type.REMOVED, al1.get(i)));
else if (point.getCol() != col) {
for (int j = col; j < point.getCol(); j++) {
col = j;
//System.out.println("ADDED " + al2.get(j));
lines.add(new LineItem(Type.ADDED, al1.get(j)));
}
col += 1;
}
if (point.getRow() == i&&point.getCol() == col) {
//System.out.println("SAME " + al1.get(point.getRow()) + " : " + al2.get(point.getCol()));
lines.add(new LineItem(Type.SAME, al1.get(i)));
p += 1;
col += 1;
}
if (i == al1.size()-1&&col!=al2.size())
for (int j = col;j < al2.size();j++)
//System.out.println("ADDED " + al2.get(j));
lines.add(new LineItem(Type.ADDED, al1.get(j)));
}
}
public static class Route {
ArrayList<Point> points;
public Route () {
points = new ArrayList<Point>();
}
public static void addPoint (int row, int col) {
boolean firstPoint = true;
int size = routes.size();
for (int k = 0;k<size;k++)
{
Route route = routes.get(k);
for (int i = route.points.size()-1; i>=0; i--)
{
if (route.points.get(i).getRow() == row)
break;
if (route.points.get(i).getRow()<row&&route.points.get(i).getCol()<col)
{
firstPoint = false;
if (i == route.points.size()-1) {
route.points.add(new Point(row, col));
}
else
{
Route r = copyRoute (route, route.points.get(i));
r.points.add(new Point(row, col));
}
break;
}
}
}
if (firstPoint)
{
Route r = new Route();
r.points.add(new Point(row, col));
routes.add(r);
}
}
public static Route copyRoute (Route route, Point point) {
Route r = new Route();
routes.add(r);
boolean endPoint = false;
for (int i = 0;i<route.points.size()-1;i++) {
r.points.add(route.points.get(i));
if (route.points.get(i).equals(point))
break;
}
return r;
}
public static Route getMaxRoute () {
Route r = null;
int max = 0;
for (Route route : routes)
{
if (route.points.size() > max) {
max = route.points.size();
r = route;
}
}
return r;
}
}
public static class Point {
int row;
int col;
public Point (int row, int col){
this.col = col;
this.row = row;
}
public int getCol() {
return col;
}
public int getRow() {
return row;
}
}
public static enum Type {
ADDED, //добавлена новая строка
REMOVED, //удалена строка
SAME //без изменений
}
public static class LineItem {
public Type type;
public String line;
public LineItem(Type type, String line) {
this.type = type;
this.line = line;
}
}
}