zoukankan      html  css  js  c++  java
  • Codevs 1230 元素查找

     时间限制: 1 s
     空间限制: 128000 KB
     题目等级 : 钻石 Diamond
    题目描述 Description

    给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出现过。

    输入描述 Input Description

    第一行两个整数 n 和m。

    第二行n个正整数(1<=n<= 100000)

    第三行m个整数(1<=m<=100000)

    输出描述 Output Description

    一共m行,若出现则输出YES,否则输出NO

    样例输入 Sample Input

    4 2

    2 1 3 4

    1 9

    样例输出 Sample Output

    YES

    NO

    数据范围及提示 Data Size & Hint

    所有数据都不超过10^8

    分类标签 Tags 

    哈希60分RE代码 head数组开小了 

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define mo1 12421
    #define mo2 34343
    using namespace std;
    
    struct node
    {
        int next;
        int to;
    }e[100001];
    int n,m,i,j,tot,head[10001];
    void add(int u,int v)
    {
        tot++;
        e[tot].next=head[u];
        e[tot].to=v;
        head[u]=tot;
    }
    int get_hash(int k)
    {
        int h=0;
        while(k)
        {
            h=h*13+k%10;
            k/=10;
        }
        return h%mo2;
    }
    bool query(int u,int v)
    {
        for(int i=head[u];i;i=e[i].next)
        {
            if(e[i].to==v)
            return true;
        }
        return false;
    }
    int main()
    {
        int a;
        cin>>n>>m;
        while(n--)
        {
            cin>>a;
            int y=get_hash(a);
            add(a,y);
        }
        while(m--)
        {
            cin>>a;
            int y=get_hash(a);
            if(query(a,y))
            cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    View Code

    哈希满分做法

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define mo1 12421
    #define mo2 34343
    using namespace std;
    
    struct node
    {
        int next;
        int to;
    }e[100001];
    int n,m,i,j,tot,head[100001];
    void add(int u,int v)
    {
        tot++;
        e[tot].next=head[u];
        e[tot].to=v;
        head[u]=tot;
    }
    int get_hash(int k)
    {
        int h=0;
        while(k)
        {
            h=h*13+k%10;
            k/=10;
        }
        return h%mo2;
    }
    bool query(int u,int v)
    {
        for(int i=head[u];i;i=e[i].next)
        {
            if(e[i].to==v)
            return true;
        }
        return false;
    }
    int main()
    {
        int a;
        cin>>n>>m;
        while(n--)
        {
            cin>>a;
            int y=get_hash(a);
            add(a,y);
        }
        while(m--)
        {
            cin>>a;
            int y=get_hash(a);
            if(query(a,y))
            cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
    }
    View Code

    STL满分做法

    #include<map>
    #include<iostream>
    using namespace std;
    int s[100001];
    map<int,bool>g;
    int main()
    {
        int n,m,ss;
        cin>>n>>m;
        for(int i=1;i<=n;++i)
        {
        cin>>s[i];
        g[s[i]]=1;
        }
        for(int i=1;i<=m;++i)
        {
            cin>>ss;
            if(g[ss]==1)
            cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
    }
    View Code

     

    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    springboot的jar为何能独立运行
    掌握SpringBoot-2.3的容器探针:实战篇
    掌握SpringBoot-2.3的容器探针:深入篇
    掌握SpringBoot-2.3的容器探针:基础篇
    详解SpringBoot(2.3)应用制作Docker镜像(官方方案)
    体验SpringBoot(2.3)应用制作Docker镜像(官方方案)
    kubespray2.11安装kubernetes1.15
    Jenkins集群下的pipeline实战
    快速搭建Jenkins集群
    前端开发神器Charles从入门到卸载
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6048935.html
Copyright © 2011-2022 走看看