import java.util.*; // this is not working /* * For each writer there is a list of readers. * a reader might have more than one writer * a writer might be a reader for another writer. * Given a list of writer (along with direct readers) * If a writer1 reads to a writer2, then Writer1's readers also * favor writer2 (transitive relation). * Output recommendation of indirect readers for each writer. * */ public class WriterClub { private List writers = new ArrayList(); private List readers = new ArrayList(); // read the input public WriterClub(String[] names){ for(String s : names){ String[] line = s.split(" "); Writer writer = new Writer (line[0]); writers.add(writer); if( !readers.contains(line[0])) readers.add(line[0]); for(int i=1;i results = new ArrayList(); for(Writer w : writers){ boolean flag = false; String writer = w.getWriter(); StringBuffer sb = new StringBuffer(); for(String r : readers){ if( r.equals(writer)) continue; if( w.isFavoriteOf(r)) continue; else if(canReach(w,r) !=0){ sb.append( r).append(" "); flag = true; } } if(sb.length() != 0 ) results.add( new Favorites( writer, sb.toString())); } Collections.sort(results); for(Favorites f: results){ System.out.println(f); } } // get direct favorite writer for specific reader public List getFavorites(String reader){ List list = new ArrayList(); for(Writer ww : writers){ if(ww.isFavoriteOf(reader)) list.add(ww); } return list; } public int canReach (Writer w, String reader){ if( w.isFavoriteOf(reader)){ return 1; }else{ List list = getFavorites(reader); if(list.isEmpty()) return 0; int s =0; for(Writer ww : list){ return s + canReach(w, ww.getWriter()); } return s; } } public static void main(String[] args){ String[][] tc ={ { "john paul ringo","alice paul john","bob alice sunny cher"}, {"tantawi taha najeeb", "aqqad najeeb ehsan", "taha aqqad najeeb"}, { "john paul ringo","alice paul john tantawi","bob alice sunny cher", "tantawi taha najeeb alice", "aqqad najeeb ehsan", "taha aqqad najeeb", } }; int count = 1; for(String[] x : tc){ System.out.println( "--- CASE " + (count++)); WriterClub wc = new WriterClub(x); wc.process(); } } } // class Writer represent each writer and direct favorable readers class Writer{ String writer; List readers = new ArrayList(); public Writer(String writer){ this.writer = writer; } public void addReader(String reader){ this.readers.add(reader); } public void addReaders(List readers){ readers.addAll(readers); } public boolean isFavoriteOf(String reader){ return readers.indexOf(reader) != -1; } public String toString(){ return writer + "<--" + readers; } public String getWriter(){ return writer;} // impelmenet equals method for searching purposes. public boolean equals(Object obj){ if(obj instanceof Writer){ Writer w = (Writer) obj; return w.writer.equals( this.writer); } return false; } } // Favorite class to be used in sorting the results. class Favorites implements Comparable{ String writer; String readers = ""; public Favorites(String writer, String readers){ this.writer = writer; String[] arr = readers.split(" "); java.util.Arrays.sort(arr); for(String str : arr) this.readers += str +" "; } public int compareTo(Favorites f){ return this.writer.compareTo(f.writer); } public String toString(){ return writer + " " + readers; } }