I am solving this challenge: https://open.kattis.com/problems/virtualfriends
My solution seems to be working but kattis's test cases are running too slowly so I was wondering how I can improve code efficiency. I am using a custom made union-find structure to do this, storing "friends" into a treemap to reference.
import java.util.*;
public class virtualfriends {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testcases = scan.nextInt();
scan.nextLine();
for (int i= 0; i < testcases; i++) {
int numFriendships = scan.nextInt();
scan.nextLine();
TreeMap<String , Integer> map = new TreeMap<String , Integer>();
int cnt = 0;
UF unionFind = new UF(50000);
for (int j = 0; j < numFriendships; j++)
{
String p1 = scan.next();
String p2 = scan.next();
if (!map.containsKey(p1)) map.put(p1, cnt++);
if (!map.containsKey(p2)) map.put(p2, cnt++);
unionFind.unify(map.get(p1), map.get(p2));
System.out.printf("%d\n", unionFind.getSetSize(map.get(p2)));
}
}
}
static class UF{
private int[] id, setSize;
private int numSets;
public UF(int size) {
id = new int[size] ;
setSize = new int[size];
numSets = size;
for(int i = 0 ; i < size ; ++i) {
id[i] = i;
setSize[i] = 1;
}
}
int find(int i )
{
int root = i;
while (root != id[root]) {
root = id[root];
}
while (i != root) {
int newp = id[i];
id[i] = root;
i = newp;
}
return root;
}
boolean isConnected(int i , int j) {
return find(i) == find(j);
}
int getNumSets() {
return numSets;
}
int getSetSize(int i) {
return setSize[find(i)];
}
boolean isSameSet(int i, int j) {
return find(i) == find(j);
}
void unify(int i, int j)
{
int root1 = find(i);
int root2 = find(j);
if (root1 == root2) return;
if (setSize[root1] < setSize[root2])
{
setSize[root2] += setSize[root1];
id[root1] = root2;
} else {
setSize[root1] += setSize[root2];
id[root2] = root1;
}
numSets--;
}
}
}
Related
I have opened an account for Ridit, one of 7-years-old students learning Java at SPOJ. The first task i gave to him was PALIN -The Next Palindrome. Here is the link to this problem- PALIN- The next Palindrome- SPOJAfter i explained it to him, he was able to solve it mostly except removing the leading zeros, which i did. Following is his solution of the problem -
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Scanner in = new Scanner(System.in);
int t = Integer.parseInt(in.nextLine());
String[] numbersInString = new String[t];
for (int i = 0; i <t; i++) {
String str = in.nextLine();
numbersInString[i] = removeLeadingZeros(str);
}
for (int i = 0 ; i<t; i++) {
int K = Integer.parseInt(numbersInString[i]);
int answer = findTheNextPalindrome(K);
System.out.println(answer);
}
}catch(Exception e) {
return;
}
}
static boolean isPalindrome(int x) {
String str = Integer.toString(x);
int length = str.length();
StringBuffer strBuff = new StringBuffer();
for(int i = length - 1;i>=0;i--) {
char ch = str.charAt(i);
strBuff.append(ch);
}
String str1 = strBuff.toString();
if(str.equals(str1)) {
return true;
}
return false;
}
static int findTheNextPalindrome(int K) {
for(int i = K+1;i<9999999; i++) {
if(isPalindrome(i) == true) {
return i;
}
}
return -1;
}
static String removeLeadingZeros(String str) {
String retString = str;
if(str.charAt(0) != '0') {
return retString;
}
return removeLeadingZeros(str.substring(1));
}
}
It is giving correct answer in Eclipse on his computer, but it is failing in SPOJ. If someone helps this little boy in his first submission, it will definitely make him very happy. I couldn't find any problem with this solution... Thank you in advance...
This might be helpful
import java.io.IOException;
import java.util.Scanner;
public class ThenNextPallindrom2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t = 0;
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()) {
t = sc.nextInt();
}
sc.nextLine();
int[] arr, arr2;
while(t > 0) {
t--;
String s = sc.nextLine();
arr = getStringToNumArray(s);
if(all9(arr)) {
arr2 = new int[arr.length + 1];
arr2[0] = 1;
for(int i=0;i<arr.length;i++) {
arr2[i+1] = 0;
}
arr2[arr2.length -1] = 1;
arr = arr2;
} else{
int mid = arr.length/ 2;
int left = mid-1;
int right = arr.length % 2 == 1 ? mid + 1 : mid;
boolean left_small = false;
while(left >= 0 && arr[left] == arr[right]) {
left--;
right++;
}
if(left < 0 || arr[left] < arr[right]) left_small = true;
if(!left_small) {
while(left >= 0) {
arr[right++] = arr[left--];
}
} else {
mid = arr.length/ 2;
left = mid-1;
int carry = 1;
if(arr.length % 2 == 0) {
right = mid;
} else {
arr[mid] += carry;
carry = arr[mid]/10;
arr[mid] %= 10;
right = mid + 1;
}
while(left >= 0) {
arr[left] += carry;
carry = arr[left] / 10;
arr[left] %= 10;
arr[right++] = arr[left--];
}
}
}
printArray(arr);
}
}
public static boolean all9(int[] arr) {
for(int i=0;i<arr.length;i++) {
if(arr[i] != 9)return false;
}
return true;
}
public static void printArray(int[] arr) {
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]);
}
System.out.println();
}
public static int[] getStringToNumArray(String s) {
int[] arr = new int[s.length()];
for(int i=0; i<s.length();i++) {
arr[i] = Integer.parseInt(String.valueOf(s.charAt(i)));
}
return arr;
}
}
I have created hashtable to solve one of the problem on hackerearth using java.
link:https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/mind-palaces-3/
My java solution is able to pass all the test cases.
Now with same logic ,I am creating solution in C.
But by solution in C does not pass all test cases.
I am learning C.
Please help me to find the problem in C code.
Thanks.
My Java Code:
package hashtable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MindPalces {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = br.readLine();
String strArr[] = string.split(" ");
int n = Integer.parseInt(strArr[0]);
int m = Integer.parseInt(strArr[1]);
SinglyLinkLists hashTable[] = new SinglyLinkLists[337];
long array[][] = new long[n][m];
for (int i = 0; i < n; i++) {
String string1 = br.readLine();
String[] innerArr = string1.split(" ");
for (int j = 0; j < m; j++) {
array[i][j] = Long.parseLong((innerArr[j]));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long data = array[i][j];
if (data < 0) {
data = data * -1;
}
int index = Hashs.hashFunction(data);
if (hashTable[index] == null) {
SinglyLinkLists linkList = new SinglyLinkLists();
linkList.insertAtEnd(i, j, array[i][j]);
hashTable[index] = linkList;
} else {
SinglyLinkLists linkList = hashTable[index];
linkList.insertAtEnd(i, j, array[i][j]);
}
}
}
int q = Integer.parseInt(br.readLine());
for (int i = 0; i < q; i++) {
long val = Long.parseLong(br.readLine());
long val1 = val;
if (val < 0) {
val = val * -1;
}
int index = Hashs.hashFunction(val);
if (hashTable[index] == null) {
System.out.println("-1" + " " + "-1");
} else {
SinglyLinkLists linkList = hashTable[index];
NOde list = linkList.getNode(val1);
if (null != list) {
System.out.println(list.getI1() + " " + list.getJ1());
} else {
System.out.println("-1" + " " + "-1");
}
}
}
}
}
class Hashs {
public static int hashFunction(long key) {
return (int) (key % 337);
}
}
class NOde {
int i1 = 0;
int j1 = 0;
NOde link = null;
long number = 0;
public NOde() {
link = null;
i1 = 0;
j1 = 0;
number = 0;
}
public NOde(NOde node, int i1, int j1, long number) {
this.i1 = i1;
this.j1 = j1;
link = node;
this.number = number;
}
public NOde getLink() {
return link;
}
public void setLink(NOde link) {
this.link = link;
}
public int getI1() {
return i1;
}
public void setI1(int i1) {
this.i1 = i1;
}
public int getJ1() {
return j1;
}
public void setJ1(int j1) {
this.j1 = j1;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
}
class SinglyLinkLists {
NOde start = null;
NOde end = null;
int size = 0;
public SinglyLinkLists() {
start = null;
end = null;
size = 0;
}
public void insertAtEnd(int i1, int j1, long number) {
NOde nptr = new NOde(null, i1, j1, number);
size++;
if (start == null) {
start = nptr;
end = start;
} else {
end.setLink(nptr);
end = nptr;
}
}
// Function to display elements
public NOde getNode(long val) {
if (start.getNumber() == val) {
return start;
}
NOde ptr = start;
ptr = start.getLink();
while (null != ptr && ptr.getLink() != null) {
if (ptr.getNumber() == val) {
return ptr;
}
ptr = ptr.getLink();
}
if (null != ptr && ptr.getNumber() == val) {
return ptr;
}
return null;
}
}
My C Code:
#include<stdio.h>
#include <stdlib.h>
struct Node
{
int i1;
int j1;
long inputVal;
struct Node * next;
};
struct hash
{
struct Node * head;
};
struct hash *hashTable=NULL;
struct Node *createNode(int i,int j,long number)
{
struct Node *list;
list = (struct Node *) malloc(sizeof(struct Node));
list->i1 = i;
list->j1 = j;
list->inputVal=number;
list->next = NULL;
return list;
};
int main()
{
int m=0,n=0;
scanf("%d%d",&m,&n);
hashTable=(struct hash *)calloc(337,sizeof(struct Node));
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
long data=0;
long copy=0;
scanf("%ld",&data);
copy=data;
if(data<0)
{
data=data*-1;
}
int index=hashFunction(data);
struct Node *newnode = createNode(i,j,copy);
struct Node *myNode = hashTable[index].head;
if (!myNode)
{
hashTable[index].head=newnode;
}
else
{
newnode->next=hashTable[index].head;
hashTable[index].head=newnode;
}
}
}
int q=0;
scanf("%d",&q);
for(int i=0; i<q; i++)
{
long val=0;
long copy=0;
int boolean=0;
scanf("%ld",&val);
copy=val;
if(val<0)
{
val=val*-1;
}
int index=hashFunction(val);
struct Node *myNode = hashTable[index].head;
if(!myNode)
{
printf("%d %d\n",-1,-1);
}
else
{
while (myNode->next!= NULL)
{
if (myNode->inputVal==copy)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
break;
}
myNode = myNode->next;
}
if(myNode->inputVal==copy &&!boolean)
{
boolean=1;
printf("%d %d\n",myNode->i1,myNode->j1);
}
if(!boolean)
{
printf("%d %d\n",-1,-1);
}
}
}
return 0;
}
int hashFunction(long data)
{
return (int)(data%337);
}
Sample Input:
5 5
-993655555 -758584352 -725954642 -696391700 -649643547
-591473088 -568010221 -432112275 -421496588 -351507172
-323741602 -232192004 -30134637 -369573 100246476
156824549 174266331 392354039 601294716 763826005
768378344 802829330 818988557 992012759 999272829
10
156824549
-758584352
-993655555
601294716
-696391700
802829330
-993655555
-232192004
392354039
-568010221
I'm trying to implement KMP algorithm. My algorithm works correctly with the following example
Text: 121121
Pattern: 121
Result: 1,4
But when Text is 12121 and pattern is the same as above, result just: 1. I don't know if this is the problem of the algorithm or of my implementation?
Other example:
Text: 1111111111
Pattern: 111
Result: 1,4,7
My code is:
public class KMP {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String text = reader.readLine();
String pattern = reader.readLine();
search(text,pattern);
}
private static void search(String text,String pattern)
{
int[] Pi = Pi(pattern);
for (int i = 0,q=0; i <text.length()&&q<pattern.length() ; i++,q++) {
while (q>=0 && pattern.charAt(q)!=text.charAt(i))
{
q=Pi[q];
}
if(q==pattern.length()-1) {
System.out.println(i-pattern.length()+2);
q=Pi[q];
}
}
}
private static int[] Pi(String p) {
int[] Pi = new int[p.length()];
Pi[0]=-1;
int i=0;
int j=-1;
while (i<p.length()-1) {
while (j>=0 && p.charAt(j)!=p.charAt(i))
{
j=Pi[j];
}
i++;
j++;
if(p.charAt(j)==p.charAt(i)) Pi[i]=Pi[j];
else Pi[i]=j;
}
return Pi;
}
}
Hope help you.
public int strStr(String source, String target) {
if (source == null || target == null){
return -1;
}
if (source.isEmpty() && !target.isEmpty()){
return -1;
}
if (source.isEmpty() && target.isEmpty()){
return 0;
}
if (target.isEmpty()){
return 0;
}
int index = 0;
int compare_index = 0;
int compare_start_index = 0;
int compare_same_length = 0;
List<Integer> answers = new ArrayList<Integer>();
while (true){
if (compare_same_length ==0){
compare_start_index = compare_index;
}
if (source.charAt(compare_index) == target.charAt(index)){
compare_same_length++;
index++;
} else {
if (compare_same_length >0){
compare_index--;
}
compare_same_length = 0;
index = 0;
}
compare_index++;
if (compare_same_length == target.length()){
answers.add(compare_start_index+1);
compare_same_length=0;
index=0;
}
if (compare_index == source.length()){
//here are answers
for (int i = 0; i < answers.size(); i++) {
int value = answers.get(i);
}
return 1;
}
}
}
I have a hard time to figure out one error after assigning
int evaluationNode = getMinDistances();
settled.add(evaluationNode);
checkNeighbours(evaluationNode);
The error show type mismatch: connot convert from Node to int. I am appreciated if anyone can help me this. Below is a complete code.
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Comparator;
public class DijkstraPriorityQueue
{
private int distances[];
private Set<Integer> settled;
private PriorityQueue<Node> priorityQueue;
private int number_of_nodes;
private int adjacencyMatrix[][];
public DijkstraPriorityQueue(int number_of_nodes)
{
this.number_of_nodes = number_of_nodes;
distances = new int[number_of_nodes + 1];
settled = new HashSet<Integer>();
priorityQueue = new PriorityQueue<Node>(number_of_nodes,new Node());
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
}
public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
int evaluationNode;
for (int i = 1; i <= number_of_nodes; i++)
for (int j = 1; j <= number_of_nodes; j++)
adjacencyMatrix[i][j] = adjacency_matrix[i][j];
for (int i = 1; i <= number_of_nodes; i++)
{
distances[i] = Integer.MAX_VALUE;
}
priorityQueue.add(new Node(source, 0));
distances[source] = 0;
while (!priorityQueue.isEmpty())
{
evaluationNode = getMinDistances();
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
}
}
private int getMinDistances()
{
int node = priorityQueue.remove();
return node;
}
private void checkNeighbours(int evaluationNode)
{
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 1; destinationNode <= number_of_nodes; destinationNode++)
{
if (!settled.contains(destinationNode))
{
if (adjacencyMatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
{
edgeDistance = adjacencyMatrix[evaluationNode][destinationNode];
newDistance = distances[evaluationNode] + edgeDistance;
if (newDistance < distances[destinationNode])
{
distances[destinationNode] = newDistance;
}
priorityQueue.add(new Node(destinationNode,distances[destinationNode]));
}
}
}
}
public static void main(String[] args)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Enter the source ");
source = scan.nextInt();
DijkstraPriorityQueue dijkstrasPriorityQueue = new DijkstraPriorityQueue(number_of_vertices);
dijkstrasPriorityQueue.dijkstra_algorithm(adjacency_matrix, source);
System.out.println("The Shorted Path to all nodes are ");
for (int i = 1; i <= dijkstrasPriorityQueue.distances.length - 1; i++)
{
System.out.println(source + " to " + i + " is " + dijkstrasPriorityQueue.distances[i]);
}
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
class Node implements Comparator<Node>
{
public int node;
public int cost;
public Node()
{
}
public Node(int node, int cost)
{
this.node = node;
this.cost = cost;
}
#Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}
In the getMinDistances method you are calling
int node = priorityQueue.remove();
But the priority queue contains Node objects, and not int values.
Maybe you wanted something like
private int getMinDistances()
{
Node node = priorityQueue.remove();
return node.getDistance();
}
but this is something that can not be answered by the debugging cloud (aka stackoverflow).
In order to use the PriorityQueue, you have to implement MinPQ extending the PriorityQueue and you'll also need a Comparator between nodes that returns the Node with a minimum distance in the MinPQ.
Take a look here for more details.
I am trying to implement Karger's min cut algorithm but I am unable to get the correct answer. Can someone please have a look at my code and help me figure out what I am doing wrong? Would really appreciate the help.
package a3;
import java.util.*;
import java.io.*;
public class Graph {
private ArrayList<Integer>[] adjList;
private int numOfVertices = 0;
private int numOfEdges = 0;
public Graph(String file) throws IOException {
FileInputStream wordsFile = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(wordsFile));
adjList = (ArrayList<Integer>[]) new ArrayList[201];
for (int i = 1; i < 201; i++) {
adjList[i] = new ArrayList<Integer>();
}
while (true) {
String s = br.readLine();
if (s == null)
break;
String[] tokens = s.split("\t");
int vertex = Integer.parseInt(tokens[0]);
this.numOfVertices++;
for (int i = 1; i < tokens.length; i++) {
Integer edge = Integer.parseInt(tokens[i]);
addEdge(vertex, edge);
this.numOfEdges++;
}
}
}
public void addEdge(int v, int w) {
adjList[v].add(w);
//this.numOfEdges++;
}
public ArrayList<Integer> getNeighbors(Integer v) {
return adjList[v];
}
public boolean hasEdge(Integer i, Integer j) {
return adjList[i].contains(j);
}
public boolean removeEdge(Integer i, Integer j) {
if (hasEdge(i, j)) {
adjList[i].remove(j);
adjList[j].remove(i);
this.numOfEdges -= 2;
return true;
}
return false;
}
public int getRandomVertex(){
Random rand = new Random();
return (rand.nextInt(this.getNumOfVertices()) + 1);
}
//Returns an array which consists of vertices connected by chosen edge
public int[] getRandomEdge(){
int arr[] = new int[2];
arr[0] = this.getRandomVertex();
while (adjList[arr[0]].size() == 0) {
arr[0] = this.getRandomVertex();
}
Random rand = new Random();
arr[1] = adjList[arr[0]].get(rand.nextInt(adjList[arr[0]].size()));
return arr;
}
//Algorithm for min cut
public int minCut() {
while (this.getNumOfVertices() > 2) {
int[] edge = this.getRandomEdge();
this.removeEdge(edge[0], edge[1]);
//Adding edges of second vertex to first vertex
for (Integer v: adjList[edge[1]]) {
if (!adjList[edge[0]].contains(v)) {
addEdge(edge[0], v);
}
}
//Removing edges of second vertex
for (Iterator<Integer> it = adjList[edge[1]].iterator(); it.hasNext();) {
Integer v = it.next();
it.remove();
this.numOfEdges--;
}
//Removing self-loops
for (Iterator<Integer> it = adjList[edge[0]].iterator(); it.hasNext();) {
Integer v = it.next();
if (v == edge[0])
it.remove();
//this.numOfEdges--;
}
this.numOfVertices--;
}
return this.numOfEdges;
}
public int getNumOfVertices() {
return this.numOfVertices;
}
public int getNumOfEdges() {
return (this.numOfEdges) / 2;
}
public String toString() {
String s = "";
for (int v = 1; v < 201; v++) {
s += v + ": ";
for (int e : adjList[v]) {
s += e + "-> ";
}
s += null + "\n";
//s += "\n";
}
return s;
}
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int min = 1000;
//Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
for (int i = 0; i < 10; i++) {
Graph test = new Graph("C:\\Users\\UE\\Desktop\\kargerMinCut.txt");
int currMin = test.minCut();
min = Math.min( min, currMin );
}
System.out.println(min);
}
}