Pagini recente » Cod sursa (job #1392538) | Cod sursa (job #2234962) | Cod sursa (job #343613) | Cod sursa (job #2727752) | Cod sursa (job #1361713)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
class Hash {
private int HASH_SIZE = 666013;
private Map<Integer, List<Integer>> hash = new HashMap<Integer, List<Integer>>();
public boolean contains(int x) {
int hx = hashFunction(x);
if (!hash.containsKey(hx)) {
return false;
}
List<Integer> chain = hash.get(hx);
return chain.contains(x);
}
public void insert(int x) {
int hx = hashFunction(x);
if (!hash.containsKey(hx)) {
hash.put(hx, new ArrayList<Integer>());
}
List<Integer> chain = hash.get(hx);
if (chain.contains(x)) {
return;
}
chain.add(x);
}
public void delete(int x) {
int hx = hashFunction(x);
if (!hash.containsKey(hx)) {
return;
}
List<Integer> chain = hash.get(hx);
int i = chain.indexOf(x);
if (i == -1) {
return;
}
chain.remove(i);
}
public int hashFunction(int x) {
return x % HASH_SIZE;
}
}
public class Main {
public static void main(String args[]) throws IOException {
InputStream inputStream = new FileInputStream("hashuri.in");
Scanner scanner = new Scanner(inputStream);
OutputStream outputStream = new FileOutputStream("hashuri.out");
PrintWriter writer = new PrintWriter(outputStream);
Hash hash = new Hash();
int N = scanner.nextInt();
while (N-- > 0) {
int type = scanner.nextInt();
int value = scanner.nextInt();
switch (type) {
case 1:
hash.insert(value);
break;
case 2:
hash.delete(value);
break;
case 3:
writer.println(String.valueOf(hash.contains(value) ? 1 : 0));
break;
}
}
writer.flush();
scanner.close();
inputStream.close();
writer.close();
outputStream.close();
}
}