Respuesta :
Answer:
Java program explained below
Explanation:
here is your files : ----------------------
Word.java : --------------
import java.util.*;
public class Word implements Comparable<Word>{
private String normal;
private String canonical;
public Word(){
normal = "";
canonical = "";
}
public Word(String norm){
setNormal(norm);
}
public void setNormal(String norm){
normal = norm;
char[] arr = norm.toCharArray();
Arrays.sort(arr);
canonical = new String(arr);
}
public String getNormal(){
return normal;
}
public String getCanonical(){
return canonical;
}
public int compareTo(Word word){
return canonical.compareTo(word.getCanonical());
}
public String toString(){
return "("+normal+", "+canonical+")";
}
}
AnagramFamily.java : ------------------------------
import java.util.*;
public class AnagramFamily implements Comparable<AnagramFamily>{
private LinkedList<Word> words;
private int size;
private class WordComp implements Comparator{
public int compare(Object o1,Object o2){
Word w1 = (Word)o1;
Word w2 = (Word)o2;
return w2.getNormal().compareTo(w1.getNormal());
}
}
public AnagramFamily(){
words = new LinkedList<>();
size = 0;
}
public LinkedList<Word> getAnagrams(){
return words;
}
public void addAnagram(Word word){
words.add(word);
size++;
}
public void sort(){
Collections.sort(words,new WordComp());
}
public int getSize(){
return size;
}
public int compareTo(AnagramFamily anag){
Integer i1 = new Integer(size);
Integer i2 = new Integer(anag.getSize());
return i2.compareTo(i1);
}
public String toString(){
return "{ Anagrams Family Size : "+size+" "+words.toString()+"}";
}
}
WordMain.java : ------------------------------
import java.util.*;
import java.io.File;
import java.io.PrintWriter;
public class WordMain{
public static void readFile(LinkedList<Word> words){
try{
Scanner sc = new Scanner(new File("words.txt"));
while(sc.hasNext()){
words.add(new Word(sc.next()));
}
sc.close();
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
public static void findAnagrams(LinkedList<AnagramFamily> anagrams,LinkedList<Word> words){
Iterator<Word> itr = words.iterator();
while(itr.hasNext()){
Iterator<AnagramFamily> aitr = anagrams.iterator();
Word temp = itr.next();
System.out.println(temp);
boolean st = true;
while(aitr.hasNext()){
AnagramFamily anag = aitr.next();
Iterator<Word> anags = anag.getAnagrams().iterator();
while(anags.hasNext()){
Word t1 = anags.next();
if(t1.compareTo(temp) == 0 && !t1.getNormal().equals(temp.getNormal())){
anag.addAnagram(temp);
st = false;
break;
}
}
anag.sort();
}
if(st){
AnagramFamily anag = new AnagramFamily();
anag.addAnagram(temp);
anagrams.add(anag);
System.out.println(anag);
}
}
}
public static void writeOutput(LinkedList<AnagramFamily> anagrams){
try{
PrintWriter pw = new PrintWriter(new File("out6.txt"));
Collections.sort(anagrams);
int i = 0;
Iterator<AnagramFamily> aitr = anagrams.iterator();
while(i < 5 && aitr.hasNext()){
AnagramFamily anag = aitr.next();
anag.sort();
pw.println(anag);
i++;
}
aitr = anagrams.iterator();
pw.println("\n\nAnagramsFamily size 8 datas : ");
while(aitr.hasNext()){
AnagramFamily anag = aitr.next();
anag.sort();
if(anag.getSize() == 8){
pw.println(anag);
}
}
pw.close();
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
LinkedList<Word> words = new LinkedList<>();
readFile(words);
Collections.sort(words);
//System.out.println(words.toString());
LinkedList<AnagramFamily> anagrams = new LinkedList<>();
findAnagrams(anagrams,words);
//System.out.println(anagrams.toString());
writeOutput(anagrams);
}
}