<Question Link>
Question:
Given N, NC, and a string, this string is composed of NC characters. Now you need to find the number of types of sub-strings with the length of N in this string.
Solution Analysis:
To judge the weight, hash these strings and map different strings to numbers. Here we convert the string to the NC hexadecimal number, different strings correspond to different numbers in NC.
1 # Include <cstdio>2 # Include <cstring> 3 Using Namespace STD; 4 Const Int M = 16e6 + 10 ; 5 Char STR [m]; 6 Bool Hash [m]; 7 Int Cur [ 256 ]; 8 9 Int Main (){ 10 Int N, NC; 11 While (Scanf ( " % D % s " , & N, & NC, STR )! = EOF ){ 12 Int CNT = 0 ; 13 Memset (cur, 0 , Sizeof (Cur )); 14 Memset (hash, False , Sizeof (Hash )); 15 Int Len = Strlen (STR ); 16 For ( Int I = 0 ; I <Len; I ++ ){ // Assign numbers to all characters 17 If (! Cur [STR [I]) 18 Cur [STR [I] = ++ CNT; 19 If (CNT = NC) Break ; 20 } 21 Int Ans = 0 ; 22 For ( Int I = 0 ; I + N- 1 <Len; I ++ ){ 23 Int Sum = 0 ; 24 For ( Int J = I; j <I + N; j ++ ){ 25 Sum + = sum * nC + cur [STR [J]; // Returns the value of this string converted to the NC hexadecimal number. 26 } 27 If (! Hash [Sum]) { 28 ++ Ans; 29 Hash [Sum] = True ; 30 } 31 } 32 Printf ( " % D \ n " , ANS ); 33 } 34 Return 0 ; 35 }
Poj 1200 Crazy Search [hash]