Skip to content

Instantly share code, notes, and snippets.

@soumitraj
Last active August 29, 2015 14:14
Show Gist options
  • Save soumitraj/ac026ff34a8332eda609 to your computer and use it in GitHub Desktop.
Save soumitraj/ac026ff34a8332eda609 to your computer and use it in GitHub Desktop.
What books to read
//Problem : Which Books to Read
//Language : Java
//Compiled Using : javac
//Version : Java 1.7.0_65
//Input for your program will be provided from STDIN
//Print out all output from your program to STDOUT
/*
Input Specifications
Your program will take from STDIN
The name of person to run comparison for.
A number D (0 ≤ N ≤ 10000) representing the degrees of separation.
A number N representing the number of social network links
A number M representing the number of users with a book list
N lines each representing a social network links. Each line will be of the format Name1|Name2 representing a link between Name1 and Name2. Names will be unique, can contain only printable ascii characters excluding '|', are case sensitive and will not exceed 100 characters.
M lines each representing a user's book list. Each line will be of the format Name|book1|book2…. Each book name will be in printable ascii excluding '|', case sensitive, and will not exceed 100 characters.
Input
Alice
2
3
4
Alice|Bob
Bob|Carol
Carol|Don
Alice|The Hobbit|Lord of the Flies
Bob|Life of Pi|A Tale of Two Cities|Lord of the Flies
Carol|Anna Karenina|Solaris|2001: A Space Odyssey
Don|Plato’s Republic|The Twelve Caesars
Output
5
Explanation
Alice knows Bob and has not read 2 of the 3 books on his list.
Bob knows Carol and Alice has not read any of the 3 books on her list.
2+3 = 5
*/
import java.util.Scanner;
import java.util.*;
//Your submission should *ONLY* use the following class name
public class Problem
{
public static void main(String[] args)
{
HashMap<String,User> userMap = new HashMap<String,User>();
Scanner stdin = new Scanner(System.in);
int lineNum=1;
String uname;
uname = stdin.nextLine();
int degreeOfFreedom = Integer.parseInt(stdin.nextLine());
int links = Integer.parseInt(stdin.nextLine());
int M_userList = Integer.parseInt(stdin.nextLine());
for(int i=0;i<links;i++){
String linkString = stdin.nextLine();
//System.out.println(linkString);
String srcUser = linkString.split("\\|")[0];
String targetUser = linkString.split("\\|")[1];
User user1, user2;
if(userMap.containsKey(srcUser))
user1 = userMap.get(srcUser);
else{
user1 = new User(srcUser);
userMap.put(srcUser,user1);
//System.out.println(srcUser);
}
if(userMap.containsKey(targetUser))
user2 = userMap.get(targetUser);
else{
user2 = new User(targetUser);
userMap.put(targetUser,user2);
//System.out.println(targetUser);
}
user1.friendList.add(user2);
user2.friendList.add(user1);
}
for(int i=0;i<M_userList;i++){
String bookListString = stdin.nextLine();
String[] bookListArray = bookListString.split("\\|");
User user = userMap.get(bookListArray[0]);;
for(int j=1;j<bookListArray.length;j++){
String token = bookListArray[j];
user.bookList.add(token);
}
}
//System.out.println("user : "+uname + "user : "+userMap.get(uname));
System.out.println( getTotalRecommendedBooks(userMap.get(uname),degreeOfFreedom));
stdin.close();
}
public static int getTotalRecommendedBooks(User user,int df){
//System.out.println(user.name);
int level = 0;
ArrayList<User> visitedUsers = new ArrayList<User>();
ArrayList<String> bookList = new ArrayList<String>();
Queue<User> q = new LinkedList<User>();
visitedUsers.add(user);
q.add(user);
for(int j=0;j<=df;j++){
Queue<User> levelQueue = new LinkedList<User>();
//System.out.println(q.size());
while (!q.isEmpty()) {
// System.out.println(q);
User v = q.remove();
// System.out.println(v.name);
for(String uBook :v.bookList){
// System.out.println(bookList);
// System.out.println(uBook);
if(!bookList.contains(uBook)){
bookList.add(uBook);
}
}
for (User w : v.friendList) {
if (!visitedUsers.contains(w)) {
// System.out.println(bookList);
// System.out.println(uBook);
// bookList.addAll(w.bookList);
visitedUsers.add(user);
levelQueue.add(w);
}
}
}
q = levelQueue;
}
int bookrecNum = bookList.size();
//System.out.println(bookrecNum);
// System.out.println(bookList);
for(String uBook :user.bookList){
if(bookList.contains(uBook)){
bookrecNum--;
}
}
return bookrecNum;
}
}
class User{
public String name;
public ArrayList<String> bookList = new ArrayList<String>();
public ArrayList<User> friendList = new ArrayList<User>();
public User(String uname){
name = uname;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment