-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHashChains.java
158 lines (137 loc) · 4.83 KB
/
HashChains.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
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
public class HashChains {
private FastScanner in;
private PrintWriter out;
// store all strings in one list
private List<String> elems;
// for hash function
private int bucketCount;
private int prime = 1000000007;
private int multiplier = 263;
private List<String>[] hashTable;
public HashChains() {
hashTable = new List[multiplier];
for (int i = 0; i < multiplier; i++) {
hashTable[i] = new ArrayList<String>();
}
}
public static void main(String[] args) throws IOException {
new HashChains().processQueries();
}
private int hashFunc(String s) {
long hash = 0;
for (int i = s.length() - 1; i >= 0; --i)
hash = (hash * multiplier + s.charAt(i)) % prime;
return (int)hash % bucketCount;
}
private Query readQuery() throws IOException {
String type = in.next();
if (!type.equals("check")) {
String s = in.next();
return new Query(type, s);
} else {
int ind = in.nextInt();
return new Query(type, ind);
}
}
private void writeSearchResult(boolean wasFound) {
out.println(wasFound ? "yes" : "no");
// Uncomment the following if you want to play with the program interactively.
// out.flush();
}
private void processQuery_naive(Query query) {
switch (query.type) {
case "add":
if (!elems.contains(query.s))
elems.add(0, query.s);
break;
case "del":
if (elems.contains(query.s))
elems.remove(query.s);
break;
case "find":
writeSearchResult(elems.contains(query.s));
break;
case "check":
for (String cur : elems)
if (hashFunc(cur) == query.ind)
out.print(cur + " ");
out.println();
// Uncomment the following if you want to play with the program interactively.
// out.flush();
break;
default:
throw new RuntimeException("Unknown query: " + query.type);
}
}
private void processQuery(Query query) {
switch (query.type) {
case "add":
if (! hashTable[hashFunc(query.s)].contains(query.s))
hashTable[hashFunc(query.s)].add(0, query.s);
break;
case "del":
if (hashTable[hashFunc(query.s)].contains(query.s))
hashTable[hashFunc(query.s)].remove(query.s);
break;
case "find":
writeSearchResult(hashTable[hashFunc(query.s)].contains(query.s));
break;
case "check":
for (String cur : hashTable[query.ind])
if (hashFunc(cur) == query.ind)
out.print(cur + " ");
out.println();
// Uncomment the following if you want to play with the program interactively.
// out.flush();
break;
default:
throw new RuntimeException("Unknown query: " + query.type);
}
}
public void processQueries() throws IOException {
elems = new ArrayList<>();
in = new FastScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
bucketCount = in.nextInt();
int queryCount = in.nextInt();
for (int i = 0; i < queryCount; ++i) {
processQuery(readQuery());
}
out.close();
}
static class Query {
String type;
String s;
int ind;
public Query(String type, String s) {
this.type = type;
this.s = s;
}
public Query(String type, int ind) {
this.type = type;
this.ind = ind;
}
}
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
}