Poj 1200 Crazy Search [hash]

Source: Internet
Author: User

<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]

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.