import java.util.ArrayList; import java.util.HashMap; import java.util.Scanner; public class trieAgain { private String isTree(String tree, int n) { int count = 0; for (int i = n; i < tree.length(); i++) { if (tree.charAt(i) == '#') { count++; } else { count--; } if (count == 1) { return tree.substring(n, i+1); } } return ""; } private boolean valid(String substring) { boolean done = false; for(int i = 0;i trees) { for (int i = 0; i < trees.size(); i++) { String def = ""; if(countChars(trees.get(i))==1){ def = trees.get(i); } HashMap maxTree = new HashMap(); int buf = 0; int counter = 0; for (int j = 1; j < trees.get(i).length(); j++) { if (trees.get(i).charAt(j) != '#') { String tree = isTree(trees.get(i), j); if(counter==0){ def = tree; } else if(countChars(tree) 0) { if (maxTree.containsKey(tree)) { int c = maxTree.get(tree); maxTree.put(tree, c+=1); } else { maxTree.put(tree, 1); } } } } String ans = ""; int maxScore = 0; for (String s : maxTree.keySet()) { int numChars = countChars(s); if ((maxTree.get(s)-1)*(numChars) > maxScore) { maxScore = (maxTree.get(s)-1)*(numChars); ans = s; } else if((maxTree.get(s)-1)*(numChars) == maxScore){ if(numChars trees = new ArrayList(); while(!entry.equals("END")){ trees.add(entry); entry = in.next(); } trieAgain inst = new trieAgain(); inst.go(trees); } }