HDU1381 UVA1123 UVALive2680 Crazy Search【哈希函数】

Crazy Search

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3149    Accepted Submission(s): 1174


Problem Description
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.

Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.

As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5.  
 

Input
The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.
 

Output
The program should output just an integer corresponding to the number of different substrings of size N found in the given text.
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

 

Sample Input
 
  
13 4daababac
 

Sample Output
 
  
5


Regionals 2002 >> Europe - Southwestern


问题链接HDU1381 UVA1123 UVALive2680 Crazy Search

问题简述:(略)

问题分析

  对于一个字符串,问其长度为n的字串有多少种?字符的种类不会超过nc种。

  这个题的一种解法是,取出所有长度为n的字串放入集合中,数一下就好了。

  另外一种解法是,用哈希函数计算各个字串的哈希值,放入集合中数一下就好了。但是,这里给出的程序没有直接使用计算哈希值的函数来实现,稍微用了一点技巧,计算时间要稍微快一些。

扫描二维码关注公众号,回复: 1731952 查看本文章

  好在,这个题测试数据不大,不需要用更多的技巧。

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序(哈希函数)如下:
/* HDU1381 UVA1123 UVALive2680 Crazy Search */

#include <iostream>
#include <string>
#include <set>

using namespace std;

#define getHashval(n, l) hv[n] - hv[n+l] * nbase[l]

typedef unsigned long long ULL;

const int SEED = 131;
const int N = 1e6;
ULL nbase[N + 1];
ULL hv[N + 1];

int main()
{
    nbase[0] = 1;
    for(int i = 1; i <= N; i++)
        nbase[i] = nbase[i-1] * SEED;

    int t, n, nc;
    cin >> t;
    while(t--) {
        set<ULL> ss;
        string s;
        cin >> n >> nc >> s;

        // 计算长度为l,各个字串的哈希值
        int len = (int)s.length();
        hv[len] = 0;
        for(int i = len - 1; i >= 0; i--)
            hv[i] = hv[i + 1] * SEED + s.at(i);

        for(int i = 0; i <= len - n; i++)
            ss.insert(getHashval(i, n));

        cout << ss.size() << endl;

        if(t)
            cout << endl;
    }

    return 0;
}



AC的C++语言程序(字串集合)如下:

/* HDU1381 UVA1123 UVALive2680 Crazy Search */

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main()
{
    int t, n, nc;
    cin >> t;
    while(t--) {
        set<string> ss;
        string s;
        cin >> n >> nc >> s;

        int len = s.length();
        for(int i = 0; i <= len - n; i++) {
            string subs = s.substr(i, n);
            ss.insert(subs);
        }

        cout << ss.size() << endl;

        if(t)
            cout << endl;
    }

    return 0;
}




猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/80521599