//Written by Tanya Khovanova and Sergei Bernstein import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; /** * ReversibleWordsFinder takes lines of text on standard input and prints * each line whose reverse exists in the input to standard output. */ public class ReversibleWordsFinder { public static void main(String[] args) throws IOException { List dictionary = new LinkedList(); Set dictionarySet = new HashSet(); BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); for(String dictWord = reader.readLine(); dictWord != null; dictWord = reader.readLine()) { dictionary.add(dictWord); dictionarySet.add(dictWord); } for (Iterator i = dictionary.iterator(); i.hasNext();) { String word = i.next(); StringBuffer wordBuffer = new StringBuffer(word); String wordBackwards = wordBuffer.reverse().toString(); if (dictionarySet.contains(wordBackwards)) System.out.println(word); } } }