• cogs 1176. [郑州101中学] 月考


    二次联通门 : cogs 1176. [郑州101中学] 月考

    /*
        cogs 1176. [郑州101中学] 月考
        
        字符串hash + 二分查找 
        
        复杂度O(N log N * K)
        其实用map可以轻松跑过 
        
        又学了个新姿势
        algorithm库中的binary_search 提供了二分查找的功能 
    */
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    
    #define Mod 26
    
    void read (int &now)
    {
        register char word = getchar ();
        for (now = 0; word < '0' || word > '9'; word = getchar ());
        for (; word >= '0' && word <= '9'; now = now * 10 + word - '0', word = getchar ());
    }
    
    int N, M;
    
    unsigned long long hash_number;
    
    #define Max 300
    char line[Max];
    
    long long hash[Max * 2000];
    
    int main (int argc, char *argv[])
    {
        freopen ("mtest.in", "r", stdin);
        freopen ("mtest.out", "w", stdout);
        
        read (N);
        
        for (register int i = 1, Len; i <= N; i ++)
        {
            scanf ("%s", line);
            Len = strlen (line);
            hash_number = 1;
            for (int pos = 0; pos < Len; pos ++)
                hash_number = hash_number * Mod + line[pos];
            hash[i] = hash_number;
        }
        std :: sort (hash + 1, hash + 1 + N);
        read (M);
        int Answer = 0;
        for (register int Len; M --; )
        {
            scanf ("%s", line);
            Len = strlen (line);
            hash_number = 1;
            for (int pos = 0; pos < Len; pos ++)
                hash_number = hash_number * Mod + line[pos];
            if (std :: binary_search (hash + 1, hash + 1 + N, hash_number))
                Answer ++;
        }
        printf ("%d", Answer);
        return 0;
    }
  • 相关阅读:
    linux设置定时任务的方法(自己总结)
    SecureCRT上传和下载文件
    ajax上传文件类型
    分页业务逻辑
    $.load
    数组中多条对象去重方式
    jquery cookie
    鼠标滚轮事件(浏览器兼容性写法)
    用cookie保存用户的登录信息,规定保存的期限
    获取url参数值
  • 原文地址:https://www.cnblogs.com/ZlycerQan/p/7197120.html
Copyright © 2020-2023  润新知