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()-1; i++) { if (tree.charAt(i) == '#') { count++; } else { count--; } if (count == 1) { if(valid(tree.substring(n,i+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++) { HashMap maxTree = new HashMap(); for (int j = 0; j < trees.get(i).length(); j++) { if (trees.get(i).charAt(j) != '#'&&trees.get(i).charAt(j)!='@') { String tree = isTree(trees.get(i), j); if (tree.length() > 0) { if (maxTree.containsKey(tree)) { int c = maxTree.get(tree); maxTree.put(tree, c+=1); } else { maxTree.put(tree, 1); } } } } String ans = ""; int maxScore = 1; for (String s : maxTree.keySet()) { int numChars = countChars(s); if (maxTree.get(s)*(numChars-1) > maxScore) { maxScore = maxTree.get(s)*(numChars-1); ans = s; } else if(maxTree.get(s)*(numChars-1) == maxScore){ if(numChars trees = new ArrayList(); while(!entry.equals("END")){ trees.add(entry); entry = in.next(); } trieAgain inst = new trieAgain(); inst.go(trees); } }