1
package com.javarush.task.task22.task2207;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
Обращенные слова
*/
public class Solution {
public static List<Pair> result = new LinkedList<>();
public static void main(String[] args) throws IOException {
StringBuilder builder=new StringBuilder();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String ss=bufferedReader.readLine();
BufferedReader reader=new BufferedReader(new FileReader(ss));
while (reader.ready()){
builder.append(reader.readLine()+" ");
}
reader.close();
String[]mas =builder.toString().split("\\s+");
for (int i = 1; i < mas.length; i++) {
if (mas[i]!=null & mas[i-1]!=null) {
String word = mas[i - 1];
StringBuilder stringBuilder = new StringBuilder(word);
stringBuilder.reverse();
String after = stringBuilder.toString();
if (after.equals(mas[i])){
Pair pair=new Pair();
pair.first=word;
pair.second=mas[i];
result.add(pair);
mas[i]=null;
}
}
}
}
public static class Pair {
String first;
String second;
public Pair(){}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
if (first != null ? !first.equals(pair.first) : pair.first != null) return false;
return second != null ? second.equals(pair.second) : pair.second == null;
}
@Override
public int hashCode() {
int result = first != null ? first.hashCode() : 0;
result = 31 * result + (second != null ? second.hashCode() : 0);
return result;
}
@Override
public String toString() {
return first == null && second == null ? "" :
first == null ? second :
second == null ? first :
first.compareTo(second) < 0 ? first + " " + second : second + " " + first;
}
}
}