Я «м делает проект в Java для университета и у меня есть вклад, как это:Мой первый проект Java
i 4 //that are the iteration
a nissan car motor -1 // "a" means add operation "nissan" is the element and "car e motor"
// are the tags associated to nissan
a apple fruit iphone macbook -1
a peach fruit color -1
s fr -1 // "s" is the subtag that I have to find in the tag
i 3
a soccer ball player -1
a volley ball shoes -1
s bal -1
END // "END" is the end of input
Выход:
2
2
Потому что на первой итерации он имеет найти два элемента, которые имеют теги, начинающиеся с «fr» , и на второй итерации он обнаружил два элемента, которые имеют теги, которые начинаются с «bal».
class main
{
private static boolean trovato;
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));
HashSet<String> totale = new HashSet <String>() ;
String line ;
String numiter;
Integer numiter2 = 0 ;
String[] parts = null ;
HashSet<String> hs = new HashSet<String> () ;
HashSet<String> tmp = new HashSet<String> () ;
Vector <Integer> conti = new Vector<Integer>();
boolean entrato = false ;
line = stdin.readLine();
while (!line.equals("<END>"))
{
if (line.startsWith("i"))
{
map.clear();
hs.clear();
totale.clear();
}
parts = line.split("[\\s\\-1]");
if (parts[0].equals("a"))
{
hs.add(parts[1]);
for (int r = 2 ; r < parts.length ; r++)
{
map.put(parts[r], hs);
}
}
System.out.println("totale" + totale.size());
if (parts[0].equals("s"))
{
entrato = false ;
for (String key : map.keySet())
{
if (key.startsWith(parts[1]))
{
entrato = true ;
totale.addAll(map.get(key));
}
}
System.out.println(totale.size());
}
line = stdin.readLine();
}
}
}
код работает, но мой учитель хочет более эффективный способ сделать this.I не знаю, как сделать это .THANKS заранее.
Помогите мне! Должен ли я использовать SortedMap r a Treemap. Что лучше для моей проблемы? – user3572461