Poj 1200 Crazy Search (hash)

Source: Internet
Author: User

Description

Please people like to solve hard puzzles some of which may lead them to madness. one such puzzle cocould be finding a hidden prime number in a given text. such number cocould be the number of different substrings of a given size that exist in the text. as you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size n that appear in the text.

As an example, consider n = 3, NC = 4 and the text "daabac ". the different substrings of size 3 that can be found in this text are: "Daa"; "AAB"; "ABA"; "Bab"; "BAC ". therefore, the answer shoshould be 5.

Input

The first line of input consists of two numbers, N and NC, separated by exactly one space. this is followed by the text where the search takes place. you may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 millions.

Output

The program shocould output just an integer corresponding to the number of different substrings of size N found in the given text.

Sample Input

3 4daababac

Sample output

5

Hint

Huge input, scanf is recommended.

Use of hash.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> using namespace STD; const int maxn = 16000000 + 100; char s [maxn]; int A [10000]; int hash [maxn]; int main () {int N, NC; while (~ Scanf ("% d", & N, & NC) {getchar (); int num = 0, sum; scanf ("% s", S ); int Len = strlen (s); For (INT I = 0; I <Len; I ++) {If (! A [s [I]) A [s [I] = ++ num; // use subscript skillfully} int CNT = 0; For (INT I = 0; I + n-1 <Len; I ++) {sum = 0; For (Int J = I; j <I + N; j ++) sum = sum * nC + A [s [J]; // you can convert it to the number in the NC base if (! Hash [Sum]) // weight {hash [Sum] = 1; CNT ++ ;}} printf ("% d \ n", CNT) ;}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.