//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; /** * SemordnilapFinder takes lines of text on standard input and prints * each line whose reverse exists in the input to standard output * and which is not a palindrome. */ public class SemordnilapFinder { 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) && !isPalindrome(word)) System.out.println(word); } } public static boolean isPalindrome(String word) { StringBuffer bufferOfWord = new StringBuffer(word); String wordBackwards = (bufferOfWord.reverse()).toString(); if (word.equals(wordBackwards)) return true; return false; } }