-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLivestockLineup.java
More file actions
140 lines (139 loc) · 3.63 KB
/
LivestockLineup.java
File metadata and controls
140 lines (139 loc) · 3.63 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.io.*;
import java.util.*;
public class LivestockLineup {
public static void main(String[] args) throws IOException
{
//Scanner scan = new Scanner(System.in);
Scanner scan = new Scanner(new File("lineup.in"));
HashMap<String, Integer> groups = new HashMap<String, Integer>();
ArrayList<ArrayList<String>> cowGroups = new ArrayList<ArrayList<String>>();
ArrayList<String> names = new ArrayList<String>(
Arrays.asList("Beatrice", "Belinda", "Bella", "Bessie", "Betsy", "Blue", "Buttercup", "Sue")); int num = scan.nextInt();
scan.nextLine();
String[] temp;
String cow1, cow2;
int index = 0;
int replace;
ArrayList<String> temp2, group1, group2;
for (int i = 0; i < num; i++)
{
temp = scan.nextLine().split(" ");
cow1 = temp[0];
cow2 = temp[5];
names.remove(cow1);
names.remove(cow2);
if (groups.containsKey(cow1) && groups.containsKey(cow2))
{
group1 = cowGroups.get(groups.get(cow1));
group2 = cowGroups.get(groups.get(cow2));
if (!cow1.equals(group1.get(group1.size()-1)))
Collections.reverse(group1);
if (!cow2.equals(group2.get(0)))
Collections.reverse(group2);
if (groups.get(cow2)<groups.get(cow1))
replace = groups.get(cow1) - 1;
else
replace = groups.get(cow1);
for (String cow: group2)
{
group1.add(cow);
}
for (String cow: group1)
{
groups.put(cow, replace);
}
cowGroups.remove(group2);
if (group1.get(0).compareTo(group1.get(group1.size()-1))>0)
Collections.reverse(group1);
}
else if (groups.containsKey(cow1))
{
groups.put(cow2, groups.get(cow1)); //connecting to group
temp2 = cowGroups.get(groups.get(cow1)); //alphabetically fixing group
if (temp2.get(0).equals(cow1)) // if the first one is cow1
{
if (cow2.compareTo(temp2.get(temp2.size()-1))<0)
temp2.add(0, cow2);
else
{
Collections.reverse(temp2);
temp2.add(temp2.size(), cow2);
}
}
else
{
if (cow2.compareTo(temp2.get(0))<0)
{
Collections.reverse(temp2);
temp2.add(0, cow2);
}
else
{
temp2.add(temp2.size(), cow2);
}
}
}
else if (groups.containsKey(cow2))
{
groups.put(cow1, groups.get(cow2)); //connecting to group
temp2 = cowGroups.get(groups.get(cow2)); //alphabetically fixing group
if (temp2.get(0).equals(cow2)) // if the first one is cow1
{
if (cow1.compareTo(temp2.get(temp2.size()-1))<0)
temp2.add(0, cow1);
else
{
Collections.reverse(temp2);
temp2.add(temp2.size(), cow1);
}
}
else
{
if (cow1.compareTo(temp2.get(0))<0)
{
Collections.reverse(temp2);
temp2.add(0, cow1);
}
else
{
temp2.add(temp2.size(), cow1);
}
}
}
else
{
groups.put(cow1, index);
groups.put(cow2, index);
index++;
if (cow1.compareTo(cow2)<0)
cowGroups.add(new ArrayList<String>(Arrays.asList(cow1, cow2)));
else
cowGroups.add(new ArrayList<String>(Arrays.asList(cow2, cow1)));
}
//System.out.println(cowGroups);
//System.out.println(groups);
}
String temp3 = "";
for (int i = 0; i < cowGroups.size(); i++) {
temp2 = cowGroups.get(i);
for (int j = 0; j < temp2.size(); j++)
{
if (j==temp2.size()-1)
temp3 += temp2.get(j);
else
temp3 += temp2.get(j) + "\n";
}
names.add(temp3);
temp3 = "";
}
Collections.sort(names);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("lineup.out")));
for (String name: names)
{
//System.out.println(name);
pw.println(name);
}
pw.close();
scan.close();
}
}