HDU 1381 Crazy Search

Source: Internet
Author: User

Crazy searchtime limit:5000msmemory limit:32768kbthis problem would be judged onHDU. Original id:1381
64-bit integer IO format: %i64d Java class name: Main Many people like-to-solve hard puzzles some of which, may leads them to madness. One such puzzle could is finding a hidden prime number in a given text. Such number could is the number of different substrings of a given size that exist in the text. As you soon would discover, you really need the "a" computer and a good algorithm to solve such a puzzle.

Your task is to write a program this given the size, N, of the substring, the number of different characters that may OCCU R in the text, NC, and the text itself, determines the number of different substrings of a size N that appear in the text.

As an example, consider n=3, nc=4 and the text "Daababac". The different substrings of size 3 that can being found in this text is: "Daa", "AaB", "ABA", "Bab", "BAC". Therefore, the answer should be 5.
InputThe first line of input consists of a numbers, N and NC, separated by exactly one space. This was followed by the text where the search takes place. Assume that the maximum number of substrings formed by the possible set of characters does is exceed.
OutputThe program should output just a integer corresponding to the number of different substrings of size N found in the given Text.
The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input
4daababac
Sample Output
5

Problem solving: very sharp hash Dafa

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMAXN =16000010;6 intbs[ the],cnt,n,Base, ans,t;7 Charstr[1000010];8 BOOLHS[MAXN];9 intMain () {Tenscanf"%d",&T); One      while(t--) { Ascanf"%d%d%s",&n,&Base, str); -         intLen =strlen (str); -memset (HS,false,sizeof(HS)); the          for(inti = CNT =0; i < Len; ++i) -             if(!Bs[str[i]]) { -Bs[str[i]] = cnt++; -                 if(CNT = =Base) Break; +             } -          for(inti = ans =0; i + n <= len; ++i) { +UnsignedintHASHV =0; A              for(intj =0; J < N; J + +) atHASHV = hashv*Base+ bs[str[i+J]]; -             if(!HS[HASHV]) { -HS[HASHV] =true; -ans++; -             } -         } inprintf"%d\n", ans); -         if(T) puts (""); to     } +     return 0; -}
View Code

HDU 1381 Crazy Search

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.