Poj1200-Crazy Search (hash entry classic), poj1200-crazyhash

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.