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;
}

 

posted @ 2017-07-17 19:21  ZlycerQan  阅读(165)  评论(0编辑  收藏  举报