• 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;
    }
  • 相关阅读:
    使用jQuery实现伪分页
    使用jQuery实现option的上移和下移
    理解Flux架构
    React 入门学习笔记1
    ES6新特性6:模块Module
    ES6新特性5:类(Class)和继承(Extends)
    ES6新特性4:字符串的扩展
    ES6新特性3:函数的扩展
    ES6新特性2:变量的解构赋值
    ES6新特性1:let和const
  • 原文地址:https://www.cnblogs.com/cax1165/p/6070920.html
Copyright © 2020-2023  润新知