codevs哈希水题

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+5,MOD=9875321,M=1e6+10;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x;
}
int n,m,mod[3]={10007,1000007,155333},a;
bool mp[3][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=1;i<=n;i++){
        a=read();
        mp[0][gethash(a,0)]=mp[1][gethash(a,1)]=mp[2][gethash(a,2)]=1;
    }
    for(int i=1;i<=m;i++){
        a=read();
        if(mp[0][gethash(a,0)]&&mp[1][gethash(a,1)]&&mp[2][gethash(a,2)])
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

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=9875321;
int n,m;
char s[1005],mp[MOD+5];
int gethash(char s[]){
    int len=strlen(s),x=0;
    for(int i=0;i<len;i++)
        x=(x*26+s[i]-'a')%MOD;
    return x%MOD;
}
int main(int argc, const char * argv[]) {
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",s);
        mp[gethash(s)]=1;
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%s",s);
        if(mp[gethash(s)]) puts("Yes");
        else puts("No");
    }
    return 0;
}

 

posted @ 2016-09-29 22:56  Candy?  阅读(281)  评论(0编辑  收藏  举报