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;
    }
  • 相关阅读:
    django rest framework serializers解读
    django rest framework通用view
    scrapy爬取伯乐在线文章
    scrapy 框架入门
    django rest framework mixins小结
    python 函数进阶
    python 变量进阶(理解)
    Docker 部署Django项目
    Docker部署Vue 工程包
    saltstack 初始化LINUX系统
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5858442.html
Copyright © 2011-2022 走看看