Reprint Please specify Source: http://www.cnblogs.com/fraud/ --by Fraud
Crazy Search
| Time Limit: 1000MS |
|
Memory Limit: 65536K |
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.
Test instructions
Give a string that asks for a total of several substrings of length n in the string
Hash. Direct, preferably double hash, Tanhashi not necessarily can.
1#include <iostream>2#include <sstream>3#include <ios>4#include <iomanip>5#include <functional>6#include <algorithm>7#include <vector>8#include <string>9#include <list>Ten#include <queue> One#include <deque> A#include <stack> -#include <Set> -#include <map> the#include <cstdio> -#include <cstdlib> -#include <cmath> -#include <cstring> +#include <climits> -#include <cctype> + using namespacestd; A #defineXinf Int_max at #defineINF 0X3FFFFFFF - #defineMP (x, y) make_pair (x, y) - #definePB (x) push_back (x) - #defineREP (x,n) for (int x=0; x<n; X + +) - #defineREP2 (X,L,R) for (int x=l; x<=r; X + +) - #defineDEP (x,r,l) for (int x=r; x>=l; x--) in #defineCLR (a,x) memset (a,x,sizeof (A)) - #defineIT iterator totypedefLong Longll; +typedef pair<int,int>PII; -typedef vector<pii>VII; thetypedef vector<int>VI; * Const intb=16000057; $ CharS[b];Panax Notoginseng BOOLA[b]; - BOOLc[ -]; themap<Char,int>m; + intMain () A { theIos::sync_with_stdio (false); + intN,nc; - while(SCANF ("%d%d", &N,&NC)! =EOF) { $scanf"%s", s); $ intlen=strlen (s); - intb=0; -Memset (A,0,sizeof(a)); the for(intI=0; i<len;i++) - {Wuyi if(c[s[i]-'a']==0) c[s[i]-'a']=1; the } - intt=0; Wu for(intI=0;i< -; i++) - { About if(C[i]) $ { -m[i+'a']=T; -t++; - } A } +b=1; the intBl=0; - //for (int i=0;i<len;i++) cout<<m[s[i]]<<endl; $ for(intI=0; i<n;i++) b= (B*NC)%B; the for(intI=0; i<n;i++) the { theBl= ((BL*NC)%b+m[s[i])%B; the } -a[bl]=1; in for(inti=n;i<len;i++) the { theBl= ((bl*nc-m[s[i-n]]*b+b)%b+m[s[i])%B; Abouta[bl]=1; the } the intans=0; the for(intI=0; i<b;i++) + { - if(A[i]) ans++; the }Bayiprintf"%d\n", ans); the } the return 0; -}code June
Poj1200crazy Search (hash)