AcWing 840. 模拟散列表-java
原题链接
简单
作者:
Susu
,
2020-01-29 14:01:04
,
所有人可见
,
阅读 993
import java.io.*;
public class Main{
static int N = 100010;
static int[] h = new int[N];
static int[] e = new int[N];
static int[] ne = new int[N];
static int idx;
static void insert(int x){
int k = (x%N+N)%N;
e[idx] = x;
ne[idx] = h[k];
h[k] = idx++;
}
static boolean find(int x){
int k = (x%N+N)%N;
for(int i=h[k];i!=-1;i=ne[i]){
if(e[i]==x) return true;
}
return false;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
for(int i=0;i<N;i++){
h[i]=-1;
}
while(n-->0){
String[] s = br.readLine().split(" ");
int x = Integer.valueOf(s[1]);
if(s[0].equals("I")){
insert(x);
}else{
if(find(x)) System.out.println("Yes");
else System.out.println("No");
}
}
}
}
import java.io.*;
public class Main{
static int N = 100003;
static int[] h = new int[N];
static int bound = (int)(1e9+1);
static int find(int x){
int k = (x%N+N)%N;
while(h[k]!=bound && h[k]!=x){
k++;
}
return k;
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(br.readLine());
for(int i=0;i<N;i++){
h[i]=bound;
}
while(n-->0){
String[] s = br.readLine().split(" ");
int x = Integer.valueOf(s[1]);
int k = find(x);
if(s[0].equals("I")){
h[k]=x;
}else{
if(h[k]!=bound) System.out.println("Yes");
else System.out.println("No");
}
}
}
}