-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task1.java
108 lines (91 loc) · 2.87 KB
/
Task1.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
/* Kornilov Nikita, M3102, 29.11.2020 */
package Sem1.Lab6;
import java.io.*;
import java.util.*;
public class Task1 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public static void main(String[] args) {
String fileName = "set";
new Task1().run(String.format("%s.in", fileName), String.format("%s.out", fileName));
}
public void solve() throws IOException {
String inputString = nextToken();
int inputValue;
MySet set = new MySet();
while (inputString != null) {
inputValue = nextInt();
switch (inputString) {
case "insert": {
set.insert(inputValue);
break;
}
case "delete": {
set.delete(inputValue);
break;
}
case "exists": {
out.print(set.exists(inputValue) ? "true\n" : "false\n");
break;
}
}
inputString = nextToken();
}
}
private class MySet {
private int capacity;
private LinkedList<Integer>[] hashTable;
private MySet() {
capacity = 9973;
hashTable = new LinkedList[capacity];
}
private void insert(Integer value) {
if (hashTable[getHash(value)] == null) {
hashTable[getHash(value)] = new LinkedList<>();
}
if (!hashTable[getHash(value)].contains(value)) {
hashTable[getHash(value)].add(value);
}
}
private void delete(Integer value) {
if (hashTable[getHash(value)] != null) {
hashTable[getHash(value)].remove(value);
}
}
private boolean exists(Integer value) {
if (hashTable[getHash(value)] != null) {
return hashTable[getHash(value)].contains(value);
}
return false;
}
private int getHash(Integer value) {
return Math.abs(value) % capacity;
}
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
String inputString = br.readLine();
if (inputString != null) {
in = new StringTokenizer(inputString);
} else {
return null;
}
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}