package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Отслеживаем изменения
*/
public class Solution {
public static List <LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (System.in));
FileReader fr1 = new FileReader (bufferedReader.readLine());
FileReader fr2 = new FileReader (bufferedReader.readLine());
bufferedReader.close();
String s1 = "";
String s2 = "";
ArrayList <String> file1 = new ArrayList <> ();
ArrayList <String> file2 = new ArrayList <> ();
Pattern p = Pattern.compile ("\\W");
while (fr1.ready ()) {
char a = (char) fr1.read();
String s = "";
s += a;
Matcher m = p.matcher (s);
if (!m.find () && !s.equals ("\r") && !s.equals ("\n")) {
s1 += s;
if (!fr1.ready())
file1.add (s1);
}
else if (!s1.equals("") || !fr1.ready()){
file1.add (s1);
s1 = "";
}
}
while (fr2.ready ()) {
char a = (char) fr2.read();
String s = "";
s += a;
Matcher m = p.matcher (s);
if (!m.find () && !s.equals ("\r") && !s.equals ("\n")) {
s2 += s;
if (!fr2.ready())
file2.add (s2);
}
else if (!s2.equals("")) {
file2.add (s2);
s2 = "";
}
}
fr1.close();
fr2.close();
first : for (int i = 0; i < file1.size () || i < file2.size ();) {
if (i == file1.size ()) {
lines.add (new LineItem (Type.ADDED, file2.get (i)));
file2.remove (file2.get (i));
continue;
}
else if (i == file2.size ()) {
lines.add (new LineItem (Type.REMOVED, file1.get (i)));
file1.remove (file1.get (i));
continue;
}
for (int j = i; j < i + 2; j++) {
if (j == i + 1) {
if (j != file2.size () && file1.get (i).equals (file2.get (j))) {
lines.add (new LineItem (Type.ADDED, file2.get (i)));
file2.remove (file2.get (i));
}
else {
lines.add (new LineItem (Type.REMOVED, file1.get (i)));
file1.remove (file1.get (i));
}
}
else if (file1.get (i).equals(file2.get (j))) {
lines.add (new LineItem (Type.SAME, file1.get (i)));
file1.remove (file1.get (i));
file2.remove (file2.get (j));
continue first;
}
}
}
for (int i = 0; i < lines.size (); i++)
System.out.println (lines.get (i).toString());
}
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;
}
public String toString () {
if (this.type == Type.SAME)
return "SAME" + " " + this.line;
else if (this.type == Type.ADDED)
return "ADDED" + " " + this.line;
else
return "REMOVED" + " " + this.line;
}
}
}package com.javarush.task.task19.task1916;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Отслеживаем изменения
*/
public class Solution {
public static List <LineItem> lines = new ArrayList<LineItem>();
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (System.in));
FileReader fr1 = new FileReader (bufferedReader.readLine());
FileReader fr2 = new FileReader (bufferedReader.readLine());
bufferedReader.close();
String s1 = "";
String s2 = "";
ArrayList <String> file1 = new ArrayList <> ();
ArrayList <String> file2 = new ArrayList <> ();
Pattern p = Pattern.compile ("\\W");
while (fr1.ready ()) {
char a = (char) fr1.read();
String s = "";
s += a;
Matcher m = p.matcher (s);
if (!m.find () && !s.equals ("\r") && !s.equals ("\n")) {
s1 += s;
if (!fr1.ready())
file1.add (s1);
}
else if (!s1.equals("") || !fr1.ready()){
file1.add (s1);
s1 = "";
}
}
while (fr2.ready ()) {
char a = (char) fr2.read();
String s = "";
s += a;
Matcher m = p.matcher (s);
if (!m.find () && !s.equals ("\r") && !s.equals ("\n")) {
s2 += s;
if (!fr2.ready())
file2.add (s2);
}
else if (!s2.equals("")) {
file2.add (s2);
s2 = "";
}
}
fr1.close();
fr2.close();
first : for (int i = 0; i < file1.size () || i < file2.size ();) {
if (i == file1.size ()) {
lines.add (new LineItem (Type.ADDED, file2.get (i)));
file2.remove (file2.get (i));
continue;
}
else if (i == file2.size ()) {
lines.add (new LineItem (Type.REMOVED, file1.get (i)));
file1.remove (file1.get (i));
continue;
}
for (int j = i; j < i + 2; j++) {
if (j == i + 1) {
if (j == file2.size () || file1.get (i).equals (file2.get (j))) {
lines.add (new LineItem (Type.ADDED, file2.get (i)));
file2.remove (file2.get (i));
}
else {
lines.add (new LineItem (Type.REMOVED, file1.get (i)));
file1.remove (file1.get (i));
}
}
else if (file1.get (i).equals(file2.get (j))) {
lines.add (new LineItem (Type.SAME, file1.get (i)));
file1.remove (file1.get (i));
file2.remove (file2.get (j));
continue first;
}
}
}
// for (int i = 0; i < lines.size (); i++)
// System.out.println (lines.get (i).toString());
}
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;
}
public String toString () {
if (this.type == Type.SAME)
return "SAME" + " " + this.line;
else if (this.type == Type.ADDED)
return "ADDED" + " " + this.line;
else
return "REMOVED" + " " + this.line;
}
}
}