private static boolean trovato , ok = false;public static void main(String args[]) throws IOException {HashMap<String, HashSet<String>> map = new HashMap<String, HashSet<String>>();BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));String line;line = stdin.readLine();String a = a ;String ii = i ;String s = s ;int cont = 0 ;while (!line.equals(<END>)) {String stringa = ;String elemento =  ;if (line.startsWith(ii)) {map = new HashMap<String, HashSet<String>>();}else if (line.startsWith(a)) {String[] letteraPerLettera = line.split();for (int i = 0  ; i < letteraPerLettera.length - 2; i++) {if (!letteraPerLettera[i].equals( )) {if (ok){element = line.substring(2,i-2) ;trovato  = true ;ok = false ;}if (trovato ){   stringa+=letteraPerLettera[i];if (map.containsKey(stringa)) {map.get(stringa).add(elemento);} else {HashSet <String> hs = new HashSet<String> () ; map.put(stringa, hs );map.get(stringa).add(elemento);}}}else {cont ++ ;if (cont == 2)ok = true ;stringa = ;}}}else if (line.startsWith(s)) {String subtag = line.substring(2,line.length()-3);if (map.containsKey(subtag)) {System.out.println(map.get(subtag).size());} else {System.out.println(missing);}}cont = 0 ; trovato = false ;line = stdin.readLine();}stdin.close();}My code has an input like this:i3a Duna automobile Deserto -1 a Nissan auto automobile -1 s aut -1i2a Pesca Sport Frutta -1s sport -1ENDi = 3 means that there are 3 lines. a means that I'm adding an element (first occurrence after a) and the other words after the element are my tags.s means that I have to find the tags that have as substring the word aut for the first block and sport for the second block.At the end the output is like this:2 12 means I have two elements duna & nissan that have tags (auto, automobile, auto) that start with the word aut.1 means I have the element pesca that has tag(sport) that starts with the sub-tag sport.With END, the program is finished.
Performance for tags for cars in Java
java