-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
263 lines (246 loc) · 9.48 KB
/
Main.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.elena.hashcode;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
public class Main {
public static List<String> slide = new ArrayList<>();
public static void main(String[] args) {
String path = " ... ";
FileReader fileReader = new FileReader(path);
try {
HashMap<String, List<String>> horizontals = fileReader.readHorizontal();
HashMap<String, List<String>> verticals = fileReader.readVertical();
HashMap<String, List<String>> photos = fileReader.readPhotos();
HashMap<String, List<String>> verticalPhotos = fileReader.readVerticalPhotos();
if(verticals.size() != 0) {
mergeVerticals(verticals, verticalPhotos, photos, horizontals);
}
createSlideShow(horizontals, photos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String outputPath = " ... ";
FileWriter fileWriter = new FileWriter(outputPath);
try {
fileWriter.print(slide);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void mergeVerticals(Map<String, List<String>> verticals,
Map<String, List<String>> verticalPhotos,
Map<String, List<String>> allPhotos,
Map<String, List<String>> horizontals) {
int size = verticalPhotos.size();
for (int i = 0; i < size - 1; i = i + 2) {
Set<String> pictures = verticalPhotos.keySet();
int minNumTags = Integer.MAX_VALUE;
String photo = "0";
for(String picture : pictures) {
if(verticalPhotos.get(picture).size() < minNumTags) {
minNumTags = verticalPhotos.get(picture).size();
photo = picture;
}
}
System.out.println("This vertical photo: " + i);
slideVerticals(photo, verticals, verticalPhotos, allPhotos, horizontals);
}
}
public static void slideVerticals(String photo,
Map<String, List<String>> verticals,
Map<String, List<String>> verticalPhotos,
Map<String, List<String>> allPhotos,
Map<String, List<String>> horizontals) {
List<String> tags = verticalPhotos.get(photo);
int min;
String minCostIndex;
int maxCost = -1;
String maxCostIndex = 0+"";
for(String tag : tags) {
List<String> neighbors = verticals.get(tag);
if(neighbors.size() != 1) {
SlideVertical slideVertical = new SlideVertical(photo, neighbors, tags, verticalPhotos);
slideVertical.verticalMerge();
min = slideVertical.getMin();
minCostIndex = slideVertical.getMinIndex();
if (min > maxCost) {
maxCost = min;
maxCostIndex = minCostIndex;
}
}
}
if(maxCost == -1 || maxCost == Integer.MAX_VALUE) {
Set<String> pictures = verticalPhotos.keySet();
int minNumTags = Integer.MAX_VALUE;
for(String picture : pictures) {
if(!picture.equals(photo)) {
if (verticalPhotos.get(picture).size() < minNumTags) {
minNumTags = verticalPhotos.get(picture).size();
maxCostIndex = picture;
}
}
}
}
tags.removeAll(verticalPhotos.get(maxCostIndex));
tags.addAll(verticalPhotos.get(maxCostIndex));
for(String tag : tags) {
if (horizontals.containsKey(tag)) {
horizontals.get(tag).add(photo+" "+maxCostIndex);
}
else {
horizontals.put(tag, new ArrayList<>());
horizontals.get(tag).add(photo+" "+maxCostIndex);
}
verticals.get(tag).remove(photo);
verticals.get(tag).remove(maxCostIndex);
if (verticals.get(tag).size() == 0) {
verticals.remove(tag);
}
}
allPhotos.put(photo+" "+maxCostIndex, tags);
allPhotos.remove(photo);
allPhotos.remove(maxCostIndex);
verticalPhotos.remove(photo);
verticalPhotos.remove(maxCostIndex);
}
public static void createSlideShow(Map<String, List<String>> horizontals,
Map<String, List<String>> photos) {
Set<String> pictures = photos.keySet();
int minNumTags = Integer.MAX_VALUE;
String photo = "0";
for(String picture : pictures) {
if(photos.get(picture).size() < minNumTags) {
minNumTags = photos.get(picture).size();
photo = picture;
}
}
slide.add(photo);
int size = photos.size();
for (int i = 0; i < size - 1; i++) {
System.out.println("This horizontal photo: " + i);
merge(slide.get(slide.size()-1), photos, horizontals);
}
}
public static void merge(String photo,
Map<String, List<String>> photos,
Map<String, List<String>> horizontals) {
List<String> tags = photos.get(photo);
int min;
String minCostIndex;
int maxCost = -1;
String maxCostIndex = 0+"";
for (String tag : tags) {
List<String> neighbors = horizontals.get(tag);
if(neighbors.size() != 1) {
Slide slide = new Slide(photo, tags, neighbors, photos);
slide.mergeAll();
min = slide.getMin();
minCostIndex = slide.getMinIndex();
if (min > maxCost) {
maxCost = min;
maxCostIndex = minCostIndex;
}
}
}
if(maxCost == -1 || maxCost == Integer.MIN_VALUE) {
Set<String> pictures = photos.keySet();
int minNumTags = Integer.MAX_VALUE;
for(String picture : pictures) {
if(!picture.equals(photo)) {
if (photos.get(picture).size() < minNumTags) {
minNumTags = photos.get(picture).size();
maxCostIndex = picture;
}
}
}
}
slide.add(maxCostIndex);
for(String tag : tags) {
horizontals.get(tag).remove(photo);
if (horizontals.get(tag).size() == 0) {
horizontals.remove(tag);
}
}
photos.remove(photo);
}
}
class SlideVertical {
public String photo;
public List<String> neighbors;
public List<String> tags;
public Map<String, List<String>> verticalPhotos;
public int maxCost = -1;
public String maxCostIndex;
public SlideVertical(String photo, List<String> neighbors, List<String> tags, Map<String, List<String>> verticalPhotos){
this.photo = photo;
this.neighbors = neighbors;
this.tags = tags;
this.verticalPhotos = verticalPhotos;
}
public void verticalMerge(){
for (String neighbor : neighbors) {
if (!neighbor.equals(photo)) {
List<String> neighborTags = verticalPhotos.get(neighbor);
List<String> tempP = new ArrayList<>();
tempP.addAll(tags);
tempP.removeAll(neighborTags);
tempP.addAll(neighborTags);
int cost = tempP.size();
if (cost > maxCost) {
maxCost = cost;
maxCostIndex = neighbor;
}
}
}
}
public int getMin(){
return maxCost;
}
public String getMinIndex(){
return maxCostIndex;
}
}
class Slide {
public String photo;
public List<String> neighbors;
public List<String> tags;
public Map<String, List<String>> photos;
public int maxCost = -1;
public String maxCostIndex;
public Slide(String photo, List<String> tags, List<String> neighbors, Map<String, List<String>> photos){
this.photo = photo;
this.neighbors = neighbors;
this.tags = tags;
this.photos = photos;
}
public void mergeAll(){
for (String neighbor : neighbors) {
if (!neighbor.equals(photo)) {
List<String> tagsN = photos.get(neighbor);
List<String> tempP = new ArrayList<>();
tempP.addAll(tags);
List<String> tempN = new ArrayList<>();
tempN.addAll(tagsN);
List<String> common = new ArrayList<>();
common.addAll(tags);
tempP.removeAll(tagsN);
int numTagsOnlyInPhoto1 = tempP.size();
tempN.removeAll(tags);
int numTagsOnlyInPhotoN = tempN.size();
common.removeAll(tempP);
int numTagsInCommon = common.size();
int cost = Math.min(numTagsInCommon, Math.min(numTagsOnlyInPhoto1, numTagsOnlyInPhotoN));
if (cost > maxCost) {
maxCost = cost;
maxCostIndex = neighbor;
}
}
}
}
public int getMin(){
return maxCost;
}
public String getMinIndex(){
return maxCostIndex;
}
}