poj-1200-hash-

Crazy Search

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 27602   Accepted: 7711

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.

Sample Input

3 4
daababac

Sample Output

5

Hint

Huge input,scanf is recommended.

Source

Southwestern Europe 2002

题目大意,输入一个子串长度和一个字符种数,然后判断该字符串中的连续子串的个数,重复的不参与重复计算。

题目思路,对每个子串进行hash就可以了。

#include<cstdio>
#include<cstring>

#define N 16000003
bool hash[N]; //保存子串是否出现过
char w[N];   //保存字符串
int id[500]; //保存字符串中每个字符的值

int main()
{
    int n,nc,i,j;
    while(~scanf("%d%d",&n,&nc))
    {

        memset(hash,false,sizeof(hash));   //hash数组初始化
        memset(id,-1,sizeof(id));
        int cnt=0;             //cnt值用来作为每个字符的哈希值
        scanf("%s",w);
        int len=strlen(w);
        for(i=0;i<len&&cnt<nc;i++)
        {

            if(id[w[i]]!=-1) continue;
            id[w[i]]=cnt++;
        }
        int ans=0;
        for(i=0;i<len-n+1;i++)
        {

            int s=0;
            for(j=i;j<i+n;j++)
            {
                s=s*nc+id[w[j]];    //每个子串的cn进制数字
            }
            if(hash[s]) continue;
            ans++;
            hash[s]=true;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-15 00:41:00

poj-1200-hash-的相关文章

POJ 1200 Crazy Search(Hash)

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 t

POJ 1200 Crazy Search (字符串hash)

题目大意: 分析长度为n的子串有多少种. 思路分析: 对于没出现的字符,将其分配一个数字. 然后将子串看做一个nc进制的数. 然后hash判断. #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <string> using namespace std; bool vis[26666

poj 1200 --- 不错的字符串HASH构造方法

题目:http://poj.org/problem?id=1200 题意:给一个字符串,给定n和nc,字符串里最多有nc个不同的字符,问长度为n的不同子串最多有几个 和上一篇现场赛那个一样,也是难在判重处理不好会超时 方法:将长度为n的子串映射为一个nc进制的数,开一个大数组,判断是否重复 #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include

Poj 1200 Crazy Search(字符串Hash)

Poj 1200 题意:给你一个n和m以及一个有m个不同字母组成的字符串,问有多少个长度为n的不同字符子串: 题解:以m为进制进行Hash.虽然是看了解题报告才会的但必须要理解并且学会运用:https://www.cnblogs.com/gj-Acit/archive/2013/05/15/3080734.html. #include<cstring>//别用set,set的添加是红黑树实现时间复杂度是O(logN),在这里会超时 #include<cstdio> #define

poj 1200 Crazy Search

题目: 链接:点击打开链接 题意: 输入n和nc,以及字符串s,输出长度为n的不同字串的个数. 算法: 思路: 用hash判重(hash值......),看了大牛的代码,对hash还是不甚理解.... 代码: #include<iostream> #include<cstring> #include<cstdio> #include<cstdio> using namespace std; #define MAXN 16000010 const int MA

poj 3156 hash+记忆化搜索 期望dp

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n,m; #define N 32 #define mod 10007LL #define mod2 10000007 #define inf 0x3fffffff typedef long long ll; typedef double dd; int f[N]

poj 3349 hash的运用

哈希函数思想在查找中是非常重要的一个思想.在数据结构中我们学习的都只是一些简单的函数 比如: 相加取余 相乘取余 相除取余 .... 哈希函数在查找中可以在O(1)时间中查找到数据的位置. 哈希函数的关键在于函数的选取 , 然而不管选择怎么样的函数 , 一般都会存在冲突 , 但是如果函数选取得得当,那么冲突就会减小. poj 3349是一题简单的hash题 我们选取的函数是: 相加取余数 sort(b , b+6 ); x = 0; for(j = 0; j < 6; j++) x = (x*1

POJ 1200 字符串HASH

题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有NC个不同的字符,所以我们可以把字符串看成是一个NC进制的串,然后计算出字符串的前缀HASH.然后枚举起点判断子串的HASH值是否已经存在.因为有了前缀HASH值,所以转移是O(1)的. #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #in

【POJ 1200】Crazy Search(将字符映射为数字,将NC进制hash成10进制)

题目链接 题目链接 http://poj.org/problem?id=1200 题意 原字符串有NC个不同字母,统计原字符串长度为N的子字符串个数 解题思路 将字符按ASCII码映射成数字. 将n个字符,即n位NC进制拼起来. 将拼起来的n位NC进制转化为10进制. 将10进制映射入hash表,每次映射判断是否已经存在. 若不存在,则ans++:否则将hash设置为存在 如何将子串(n位NC进制)映射为10进制 a = 0 b = 1 c = 2 则 cbaa = 2 * 3^3 + 1 *

poj 1200 (hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted: 6513 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 gi