2015-08-19
Test instructions: Give two number N,NC, and give a string consisting of NC characters. Find out how many substrings of length n are in this string.
Analysis:
1. This problem does not need to be matched because it is not efficient.
2. Consider a substring of length n as an n-bit NC number and convert the problem to a total number of decimal digits.
3. When hashing, each character corresponds to a number of 0---nc-1.
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <iostream>5#include <map>6 using namespacestd;7 Const intMAXN =16e6;8 9 CharS[MAXN];TenUnsignedintHASH[MAXN]; One intn,m; A intname[ +]; - -UnsignedintHashChar*STR,intx) the { -UnsignedintAns =0; - for(inti =0; i < x; ++i) - { +Ans=ans*m +name[* (str+i)]; - } + returnans; A } at - intMain () - { - //freopen ("in", "R", stdin); - while(~SCANF ("%d%d",&n,&m)) - { inscanf"%s", s); - intLen = strlen (s), k =0; to for(inti =0; i < Len; ++i) + { - if(Name[s[i]] = =0) Name[s[i]] = k++; the } * for(inti =0; I <= len-n; ++i) $ {Panax NotoginsengUnsignedintID = Hash (s+i,n); -Hash[id] =1; the } + intAns =0; A for(inti =0; i < MAXN; ++i) theAns + =Hash[i]; +printf"%d\n", ans); - } $}
View Code
POJ-1200 (hash)