-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutocomplete_MiS.java
More file actions
96 lines (82 loc) · 2.95 KB
/
Autocomplete_MiS.java
File metadata and controls
96 lines (82 loc) · 2.95 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
/**
@mseskar
@11/29/17 (cutting it close lol)
a program to implement autocomplete for a given set of N terms,
where a term is a query string and an associated nonnegative weight.
That is, given a prefix, find all queries that start with the given prefix,
in descending order of weight.
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Autocomplete_MiS {
private Term[] a;
public Autocomplete_MiS(Term[] terms) {
if(terms == null || !checkNull(terms))
throw new NullPointerException("Input array null or array items are null");
this.a = terms;
Merge.sort(a);
}
private static boolean checkNull(Term[] a) {
for(int i = 0; i < a.length; i++)
if(a[i] == null)
return false;
return true;
}
public Term[] allMatches(String prefix) {
if(prefix == null)
throw new NullPointerException("Prefix is null");
int num = numberOfMatches(prefix);
Term[] matches = new Term[num];
int first = first(a, prefix);
int last = last(a, prefix);
for(int i = first, k = 0; i <= last; i++, k++)
matches[k] = a[i];
Merge.sort(matches, Term.byReverseWeightOrder());
return matches;
}
public int numberOfMatches(String prefix) {
if(prefix == null)
throw new NullPointerException("Prefix is null");
int first = first(a, prefix);
int last = last(a, prefix);
return last - first + 1;
}
/*
Binary search to find first reference of prefix, and last reference to save time. As suggested by the Princeton guy
*/
private int first(Term[] a, String prefix) {
return BinarySearch.firstIndexOf(a, new Term(prefix, 0), Term.byPrefixOrder(prefix.length()));
}
private int last(Term[] a, String prefix) {
return BinarySearch.lastIndexOf(a, new Term(prefix, 0), Term.byPrefixOrder(prefix.length()));
}
public static void main(String[] args) {
File f = new File(args[0]);
Scanner in = null;
try {
in = new Scanner(f);
} catch(FileNotFoundException e) {
System.err.println("FileNotFoundException: " + e.getMessage());
}
int n = in.nextInt();
Term[] terms = new Term[n];
for(int i = 0; i < terms.length; i++) {
long weight = in.nextLong();
String q = in.nextLine();
String query = q.trim();
terms[i] = new Term(query, weight);
}
in.close();
Scanner user = new Scanner(System.in);
int k = Integer.parseInt(args[1]);
Autocomplete_MiS autocomplete = new Autocomplete_MiS(terms);
while (user.hasNextLine()) {
String prefix = user.nextLine();
Term[] results = autocomplete.allMatches(prefix);
for (int i = 0; i < Math.min(k, results.length); i++)
System.out.println(results[i]);
}
user.close();
}
}