Poj1200-Crazy Search (hash entry classic), poj1200-crazyhash
Hash: generally an integer. That is to say, an algorithm can compress a string into an integer.
I. Question:
Two numbers n and nc are given, and a string consisting of nc characters is given. How many types of different substrings whose length is n in this string?
Ii. Ideas:
1. This question does not need to be matched because it is not efficient.
2. Regard the substring whose length is n as the n-bit nc hexadecimal number, and convert the problem into the total number of decimal numbers.
3. During hash, each character corresponds to the 0 ~ A number for the nc-1.
Step 3:
1. Enter the nc Letter Number: 0 ~ Nc-1
HashArray [ch [I] = k ++;
2. Specify that each n letters ch [I] correspond to an n-bit nc hexadecimal number hashArray [ch [I], for example, abb --- 011;
3. Convert the nc Number of hashArray [] into a decimal integer sum and mark it with lage [sum] = true.
4. count the number of different substrings.
1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 const int MaxNum = 20000000; 5 char ch [MaxNum]; 6 bool lage [MaxNum]; // used to mark whether it is the same substring 7 int hashArray [256]; // store n letters into integers and convert them into numbers 8 9 int main () in nc notation () {10 int n, nc; 11 while (cin> n> nc> ch) {12 int k = 0; 13 int len = strlen (ch ); // note 14 for (int I = 0; I <len; I ++) {15 if (hashArray [ch [I] = 0) {16 hashArray [ch [I] = k ++; // give the nc Letter numbers, such as hashArray ['a'] = 117} 18} 19 int ans = 0; // records the number of different substrings 20 for (int I = 0; I <= len-n; I ++) {21 int sum = 0; 22 for (int j = I; j <I + n; j ++) {23 sum = sum * nc + hashArray [ch [j]; // convert the nc hexadecimal number of hashArray [] into a decimal integer sum24} 25 if (! Lage [sum]) {// The value is false26 ans ++; 27 lage [sum] = true; // true28} 29} 30 cout <ans <endl; 31} 32 return 0; 33}View Code
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.