AcWing 840. 模拟散列表
原题链接
简单
作者:
ywy
,
2021-01-14 12:48:37
,
所有人可见
,
阅读 223
import java.util.Arrays;
import java.util.Scanner;
/**
* @author hsdhebbd
* @create 2021-01-14 12:33
* JAVA用数组模拟哈希表,采用开放寻址法
*/
public class Main {
private static final int N = 200003;
private static int hash[] = new int[N];
private static int null = (int)(1e9+1);
public static int find(int val){
int index = (val % N + N) % N;
while (hash[index] != null && hash[index] != val){
index ++;
if(index >= N) index = 0;
}
return index;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Arrays.fill(hash,null);
String key;
int x;
while(n-- > 0){
key = scanner.next();
x = scanner.nextInt();
if(key.equals("I")){
hash[find(x)] = x;
}else{
int k = find(x);
if(hash[k] == x) System.out.println("Yes");
else System.out.println("No");
}
}
}
}