-
Notifications
You must be signed in to change notification settings - Fork 19
/
baconeggsandspam.java
61 lines (50 loc) · 1.52 KB
/
baconeggsandspam.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
public class baconeggsandspam {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true)
{
int num = scan.nextInt();
HashMap<String , ArrayList<String>> map = new HashMap<>();
ArrayList<String> ingredients = new ArrayList<>();
if (num == 0)
break;
for (int zax = 0; zax < num; zax++)
{
String name = scan.next();
String[] ing = scan.nextLine().substring(1).split(" ");
for (int i = 0; i < ing.length; i++)
{
if (!map.containsKey(ing[i]))
{
ArrayList<String> in = new ArrayList<>();
in.add(name);
map.put(ing[i] , in);
ingredients.add(ing[i]);
}
else
{
ArrayList<String> in = map.get(ing[i]);
in.add(name);
map.put(ing[i] , in);
}
}
}
Collections.sort(ingredients);
for (int i = 0; i < ingredients.size(); i++)
{
System.out.print(ingredients.get(i));
ArrayList<String> names = map.get(ingredients.get(i));
Collections.sort(names);
for (int z = 0; z < names.size(); z++)
System.out.print(" " + names.get(z));
System.out.println();
}
System.out.println();
}
scan.close();
}
}