codevs哈希水题

时间:2023-11-28 13:37:32

1230

多重hash练习一下,不用也可以

//
// main.cpp
// codeves1230
//
// Created by Candy on 9/29/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=1e5+,MOD=,M=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,mod[]={,,},a;
bool mp[][M];
int gethash(int x,int p){
return x%mod[p];
}
int main(int argc, const char * argv[]) {
n=read();m=read();
for(int i=;i<=n;i++){
a=read();
mp[][gethash(a,)]=mp[][gethash(a,)]=mp[][gethash(a,)]=;
}
for(int i=;i<=m;i++){
a=read();
if(mp[][gethash(a,)]&&mp[][gethash(a,)]&&mp[][gethash(a,)])
printf("YES\n");
else printf("NO\n");
}
return ;
}

2875

字符串哈希,26进制

//
// main.cpp
// codevs2875
//
// Created by Candy on 9/29/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MOD=;
int n,m;
char s[],mp[MOD+];
int gethash(char s[]){
int len=strlen(s),x=;
for(int i=;i<len;i++)
x=(x*+s[i]-'a')%MOD;
return x%MOD;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",s);
mp[gethash(s)]=;
}
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%s",s);
if(mp[gethash(s)]) puts("Yes");
else puts("No");
}
return ;
}