How can I use a CyclicBarrier
with N-THREAD according to my core for optimize this code?
My code has an input like this:
i 3 a duna automobile deserto -1 a nissan auto automobile -1 s aut -1 i 2 a pesca sport frutta -1 s sport -1 END
i = 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 1
- "2" means that I have two elements "duna" & "nissan" that have tags (auto, automobile, auto) that start with the word "aut".
- "1" means that I have the element "pesca" that has tag (sport) that starts with the sub-tag "sport".
With END
, the program is finished.
package Prove;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
class FinalMap {
public static void main(String args[]) throws IOException {
HashMap<String, Set<String>> map = new HashMap<String, Set<String>>();
java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
java.io.BufferedReader stdin = new java.io.BufferedReader(isr, 16 * 1024);
String line = stdin.readLine();
final String ADD_COMMAND = "a";
final String ITEM_COMMAND = "i";
final String SEARCH_COMMAND = "s";
final String END_MARKER = "<END>";
while (!line.equals("<END>")) {
String elemento = "" ;
if (line.startsWith(ITEM_COMMAND))
{
map = new HashMap<String, Set<String>>();
}
else if (line.startsWith(ADD_COMMAND)) {
String[] parts = line.split("\\s+");
for (int i = 2; i < parts.length - 1; ++i) {
String element = parts[1];
String tag = parts[i];
for (int j = 1; j <= parts[i].length(); ++j) {
String tagPrefix = tag.substring(0, j);
Set<String> elements = map.get(tagPrefix);
if (elements == null) {
elements = new HashSet<String>();
map.put(tagPrefix, elements);
}
elements.add(element);
}
}
}
else if (line.startsWith(SEARCH_COMMAND)) {
String subtag = line.substring(2,line.length()-3);
if (map.containsKey(subtag)) {
System.out.println(map.get(subtag).size());
} else {
System.out.println("missing");
}
}
line = stdin.readLine();
}
stdin.close();
}
}