zoukankan      html  css  js  c++  java
  • RY哥查字典(字符串双模hash初步)

    RY哥查字典

    题目描述:
    RY哥最近新买了一本字典,他十分高兴,因为这上面的单词都十分的和谐,他天天查字典。
    输入描述:
    1个整数N,表示字典里面的单词数量。
    接下来N行,每行一个字符串,表示一个单词。
    然后第N+2行,一个整数M,表示要查的单词数。
    接下来M行,每行一个字符串,表示一个要查的单词。
    输出描述:
    对于每一个要查的单词,如果在字典里面,就输出’Yes’,否则输出’No’,一行一个。
    样例输入:
    2
    i
    you
    1
    love
    样例输出:
    No
    数据范围及提示:
    1

    #include<iostream>
    #include<cstring>
    using namespace std;
    const int maxn=10010;
    const int f1=13;
    const int f2=17;
    struct node
    {
        int to;
        int next;
    }e[maxn];
    int n,tot,head[maxn];
    char c[110];
    int get_hash_1(char s[110])
    {
        int tmp=0;
        for(int i=0;i<strlen(s);i++)
        tmp=(tmp+(s[i]-96)*f1%10007);
        return tmp%10007;
    }
    int get_hash_2(char s[110])
    {
        int tmp=0;
        for(int i=0;i<strlen(s);i++)
        tmp=(tmp+(s[i]-96)*f2%521);
        return tmp%521;
    }
    void add_edge(int u,int v)
    {
        tot++;
        e[tot].to=v;
        e[tot].next=head[u];
        head[u]=tot;
    }
    bool find(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 x,y;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>c;
            x=get_hash_1(c);
            y=get_hash_2(c);
            add_edge(x,y);
        }
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>c;
            x=get_hash_1(c);
            y=get_hash_2(c);
            if(find(x,y))
            cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    C语言左移和右移
    mmap详谈
    eclipse插件自动生成类图
    async 和 defer 的区别
    SVN里恢复到某一天的版本操作
    解决跨域的jsonp+Java实例
    HTTP请求行、请求头、请求体等
    ajax在什么情况下会走success和error
    记阅读POST与GET的区别
    记一些快捷键
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070920.html
Copyright © 2011-2022 走看看