poj 1200 Crazy Search

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

这道题使用了hash,hash早都听说了一直没怎么用过。

View Code
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define  M  20000000
 4 int hash[M];
 5 char str[1600001];
 6 int word_map[256],hash_add=0;
 7 int n=0,nc=0;
 8 
 9 int strhash(char * key)
10 {
11     int i,h=0;
12     for (i=0;i<n;i++)
13     {
14         h = h*nc + word_map[*(key + i)];
15     }
16     return h%M;
17 }
18 int main()
19 {
20     int i=0,sizeNC=0,count=0;
21     scanf("%d %d",&n,&nc);
22     sizeNC = nc;
23     scanf("%s",str);
24     while (sizeNC)
25     {
26         if (!word_map[str[i]])
27         {
28             word_map[str[i]] = sizeNC;
29             sizeNC--;
30         }
31         i++;
32     }
33 
34     i=0;
35 
36     while (str[i+n-1]!='\0')
37     {
38         hash_add = strhash(str+i);
39         if (!hash[hash_add])
40         {
41             hash[hash_add]++;
42             count++;
43         }
44         i++;
45     }
46     printf("%d\n",count);
47     return 0;
48 }

 

posted @ 2012-05-06 15:43  我们一直在努力  阅读(159)  评论(0编辑  收藏  举报