Poj 1200 Crazy Search (hash ).

Source: Internet
Author: User

~~~~

It turns out that the time-out is when the map-based violence is used. I used to use hash. The first time I wrote hash, it was really a fun thing.

Question link: http://poj.org/problem? Id = 1200

Note: 1. The question text is composed of characters, so the ascll code is used as the subscript.

2. the hash array must be larger, otherwise re.


# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # include <cmath> # define n 16000000 using namespace STD; int Q [200]; int hash [N]; char STR [N]; int main () {int n, m; while (~ Scanf ("% d", & N, & M) {int val =-1, TOT = 0; scanf ("% s", STR ); int Len = strlen (STR); memset (Q, 0, sizeof (q); memset (hash, 0, sizeof (hash )); // assign values to each character using the ascll code as the subscript. For (INT I = 0; I <Len; I ++) if (! Q [STR [I]) Q [STR [I] = ++ val; // each string with a length of N is given. For (INT I = 0; I <= len-N; I ++) {int S = 0; For (Int J = I; j <I + N; j ++) S + = S * m + Q [STR [J]; // map the hash function. If (! Hash [s]) // as to why do we use a hexadecimal-like ing function, please instruct seller Meng. {Tot ++; hash [s] = 1 ;}} printf ("% d \ n", TOT) ;}return 0 ;}


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.