Crazy Search
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 27602 |
|
Accepted: 7711 |
Description
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 is found in this text is: "DAA"; "AaB"; "ABA"; "Bab"; "BAC". Therefore, the answer should be 5.
Input
The 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.
Output
The program should output just a 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.
Source
Southwestern Europe 2002, enter a substring length and a number of characters, then determine the number of successive substrings in the string, and repeat the repetition. The idea of the topic, the hash of each sub-string can be.
#include <cstdio>#include<cstring>#defineN 16000003BOOLhash[n];//save substring has occurredCharW[n]; Save Stringintid[ -]; Save the value of each character in a stringintMain () {intn,nc,i,j; while(~SCANF ("%d%d",&n,&NC)) {memset (hash,false,sizeof(hash)); Hash array initialization memset (ID,-1,sizeof(ID)); intCnt=0; The CNT value is used as the hash value for each character scanf ("%s", W); intlen=strlen (W); for(i=0; i<len&&cnt<nc;i++) { if(id[w[i]]!=-1)Continue; Id[w[i]]=cnt++; } intans=0; for(i=0; i<len-n+1; i++) { ints=0; for(j=i;j<i+n;j++) {s=s*nc+Id[w[j]]; CN binary number of each substring}if(Hash[s])Continue; Ans++; Hash[s]=true; } printf ("%d\n", ans); } return 0;}
poj-1200-hash-