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

题意:输入n,m,和字符窜,给你字符串出现字母的种数(m),问你长度为n的字符串有多少种?

hash,看代码吧

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define N 16000005

char a[N];
int n,m;
int ha[N];

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

        i=0;
        memset(ha,0,sizeof(ha));

        int len=strlen(a);

        for(j=0;j<len;j++)
        {
            if(!ha[a[j]])
            {
                ha[a[j]]=++i;
                if(i==m)
                    break;
            }
        }

        int ans=0;

        for(i=0;i+n<=len;i++)
        {
            int temp=0;
            for(j=i;j<i+n;j++)
            {
                temp=temp*m+ha[a[j]];
            }
            if(!ha[temp])
            {
                ans++;
                ha[temp]=1;
            }
        }

        printf("%d\n",ans);
        return 0;
    }
    return 0;
}
时间: 2024-11-05 11:29:27

poj 1200 (hash)的相关文章

哈希(Hash)与加密(Encrypt)相关内容

1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可逆的密文. i.哈希算法往往被设计成生成具有相同长度的文本,而加密算法生成的文本长度与明文本身的长度有关. 例:设我们有两段文本:"Microsoft"和"Google".两者使用某种哈希算法得到的结果分别为:"140864078AECA1C7C35B4BEB

POJ 2253-Frogger (Prim)

题目链接:Frogger 题意:两只青蛙,A和B,A想到B哪里去,但是A得弹跳有限制,所以不能直接到B,但是有其他的石头作为过渡点,可以通过他们到达B,问A到B的所有路径中,它弹跳最大的跨度的最小值 PS:最小生成树过的,刚开始用Dijstra做,Kao,精度损失的厉害,对于Dijksra的变形不大会变啊,看了Discuss有人用最小生成树过,我一划拉,还真是,敲了,就过了,等会研究研究最短路的各种变形,有模板不会变,不会灵活应用,渣渣就是渣渣. ME               Time 10

poj 1861(最小生成树)

Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access

hdu 4886 TIANKENG’s restaurant(Ⅱ) (hash)

题目大意: 求出在文本串中第一个没出现的字典序最小的串.. 思路分析: 开始的时候 用后缀数组写,然后根据sa的有序性.你就可以知道哪个串没有出现了. 但是题目卡了倍增哦... 自习想一想的话,我们用 sa 数组,也就是想知道这个串有没有出现过,也就是判断重复,却浪费了  O (n * lg n)... 判断重复为什么没想到hash . 把每一个长度的子串都hash 出来,用八进制表示,这样的话就可以得到一个递增的hash值. 将之存入hash 数组.找到第一个空的hash的下标,就是第一个没出

POJ 2352 (stars)

[题意描述] 就是给定n个星星的x,y坐标,y坐标按照从小到大的顺序进行排列,x坐标随机排列.下面求对于每个星星而言,其它星星的x,y的坐标都小于等于该星星的数目,然后输出所有的情况. [思路分析] 我们这道题可以采用树状数组求解,将x+1作为树状数组的底标. [AC代码] #include<iostream> #include<bitset> #include<cstdio> #include<cstring> #include<algorithm&

poj Sudoku(数独) DFS

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13665   Accepted: 6767   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

HDU 4821 (hash)

这道题最重要的不仅是hash这种算法,更要学会利用好STL中的<map>才行. 将连续的L个字符经过hash赋值,最后线性判断.其中的判断步骤用到了map的插入特性. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <map> using namespace std; #define maxn 500010 #

POJ 3419 (rmq)

这道题是rmq,再加上一个解决溢出. 刚开始我也想过用rmq,虽然不知道它叫什么,但是我知道应该这样做.可是后来没想到这道题的特殊性,也就是解决溢出的方法,就放弃了. rmq可以用线段树,也可以用dp.  这道题都可以过的,而且线段树要快一些. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; #define m

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (