zoukankan      html  css  js  c++  java
  • 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
    ");
            else printf("NO
    ");
        }
        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;
    }
  • 相关阅读:
    next_permitation
    POJ 1979 Red and Black
    POJ 2386 Lake Counting
    BFS简单迷宫
    部分和问题
    图论复习--二分图判断
    danci1
    danci
    jquery事件重复绑定解决办法
    danci
  • 原文地址:https://www.cnblogs.com/candy99/p/5922289.html
Copyright © 2011-2022 走看看