zoukankan      html  css  js  c++  java
  • Poj(1220),hash

    题目链接:http://poj.org/problem?id=1200

    这个题,我真是无限MLE,RE,WA,太伤心了,还是写一下吧。题意很简单(英语很好读),最后看了一下金海峰的思路。果然,应该是我的这个hash表有点问题,最好是用正确的算法吧,不乱创造了。karp-rabin把字符串转化成数字的算法,一个字符串有n种字符构成,把每种字符对应为0~n-1中的一个数字,把字母换成对应的数字之后,对于固定长度的串,每个串都与一个唯一的n进制数对应。这样就可以hash了。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    #define maxn 16000005
    
    bool hash[maxn];
    int name[260];
    int n, nc;
    char st[maxn];
    
    int main()
    {
        //freopen("t.txt", "r", stdin);
        scanf("%d%d%s", &n, &nc, st);
        memset(name, 0, sizeof(name));
        memset(hash, 0, sizeof(hash));
        int len = strlen(st);
        int t =0;
        for (int i =0; i < len; i++)
            if (name[st[i]] ==0)
                name[st[i]] = t++;
        int temp =0;
        t = nc;
        for (int i =0; i < n -1; i++)
        {
            temp = temp * nc + name[st[i]];
            t *= nc;
        }
        int ans =0;
        for (int i = n -1; i < len; i++)
        {
            temp = (temp * nc + name[st[i]]) % t;
            if (!hash[temp])
            {
                ans++;
                hash[temp] =true;
            }
        }
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    java 字符流
    大兴安岭300余幅岩画得到及时保护
    [Hive]-Table
    [Hive]-DataBase
    [Hive]-列式存储篇
    [Hive]-架构篇
    [Hive]编译部署-hive-1.1.0-cdh5.7.0
    [Hadoop]-MapReduce-使用篇
    [Hadoop]MapReduce-架构篇
    [Hadoop]-常用命令
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5858442.html
Copyright © 2011-2022 走看看