Skip to content

Instantly share code, notes, and snippets.

@wingyplus
Created July 24, 2011 06:32
Show Gist options
  • Save wingyplus/1102329 to your computer and use it in GitHub Desktop.
Save wingyplus/1102329 to your computer and use it in GitHub Desktop.
MovieMania
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("b.in");
Scanner scan = new Scanner(file);
List<String> list = new ArrayList<String>();
int loop = scan.nextInt();
for (int i = 1; i <= loop; i++) {
System.out.println("case_" + i);
int line = scan.nextInt();
int words = scan.nextInt();
for (int j = 1; j <= words; j++) {
list.add(scan.next());
}
scan.nextLine(); // Flush `\n`
HashSet<String> recommend = new HashSet<String>();
HashSet<String> unrecommend = new HashSet<String>();
Map<String, HashSet<String>> rec = new HashMap<String, HashSet<String>>();
Map<String, HashSet<String>> unrec = new HashMap<String, HashSet<String>>();
// Contains all Movie
for (int k = 0; k < line; k++) {
String str = scan.nextLine();
for (int j = 0; j < list.size(); j++) {
HashSet<String> cache;
if (str.indexOf(list.get(j)) < 0)
unrecommend.add(str.split(" ")[0]);
else
recommend.add(str.split(" ")[0]);
if (rec.containsKey(list.get(j))) {
cache = rec.get(list.get(j));
cache.addAll(recommend);
cache = unrec.get(list.get(j));
cache.addAll(unrecommend);
} else {
rec.put(list.get(j), new HashSet<String>(recommend));
unrec.put(list.get(j), new HashSet<String>(unrecommend));
}
unrecommend.clear();
recommend.clear();
}
}
// Print Result
List<String> k = new ArrayList<String>(unrec.keySet());
Collections.sort(k);
for(String key : k) {
System.out.print(key + "-");
if(rec.get(key).size() == 0 || rec.get(key).size() == 1 || rec.get(key).size() == line) System.out.print("null");
else {
List<String> sort = new ArrayList<String>(unrec.get(key));
Collections.sort(sort);
for(String movie : sort) {
System.out.print(movie + " ");
}
}
System.out.println();
}
}
}
}
@wingyplus
Copy link
Author

Test Case (FILE : b.in)

3
4 6 Bat Ant Dog Egg Fat Cat
Avatar Bat Ant Cat Egg
Transformer Cat Egg Ant Dog Bat
Inception Dog Egg Ant Cat
HarryPotter Fat Ant Bat Dog
5 6 Cat Bat Dog Fat Egg Ant
Avatar Ant Dog Fat
Transformer Bat Fat Ant
Inception Cat Bat
HarryPotter Dog Cat
T2 Egg Cat Fat
4 6 Bat Fat Ant Egg Cat Dog
Avatar Egg Fat
Transformer Ant Bat Dog
Inception Bat Ant Cat Dog
T2 Dog Bat Ant Cat

OUTPUT (std.out)

case_1
Ant-null
Bat-Inception
Cat-HarryPotter
Dog-Avatar
Egg-HarryPotter
Fat-null
case_2
Ant-HarryPotter Inception T2
Bat-Avatar HarryPotter T2
Cat-Avatar Transformer
Dog-Inception T2 Transformer
Egg-null
Fat-HarryPotter Inception
case_3
Ant-Avatar
Bat-Avatar
Cat-Avatar Transformer
Dog-Avatar
Egg-null
Fat-null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment