zoukankan      html  css  js  c++  java
  • poj1200Crazy Search (哈希)

    转载请注明出处: 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 lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could 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 "daababac". The different substrings of size 3 that can be found in this text are: "daa"; "aab"; "aba"; "bab"; "bac". Therefore, the answer should 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 should output just an integer corresponding to the number of different substrings of size N found in the given text.

    Sample Input

    3 4
    daababac

    Sample Output

    5

    Hint

    Huge input,scanf is recommended.

    题意

    给一个字符串,问在该串中总共出现了几种长度为n的子串

    哈希。直接搞,最好是双哈希,单哈希不一定能过。

     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>
    10 #include <queue>
    11 #include <deque>
    12 #include <stack>
    13 #include <set>
    14 #include <map>
    15 #include <cstdio>
    16 #include <cstdlib>
    17 #include <cmath>
    18 #include <cstring>
    19 #include <climits>
    20 #include <cctype>
    21 using namespace std;
    22 #define XINF INT_MAX
    23 #define INF 0x3FFFFFFF
    24 #define MP(X,Y) make_pair(X,Y)
    25 #define PB(X) push_back(X)
    26 #define REP(X,N) for(int X=0;X<N;X++)
    27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
    28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
    29 #define CLR(A,X) memset(A,X,sizeof(A))
    30 #define IT iterator
    31 typedef long long ll;
    32 typedef pair<int,int> PII;
    33 typedef vector<PII> VII;
    34 typedef vector<int> VI;
    35 const int B=16000057;
    36 char s[B];
    37 bool a[B];
    38 bool c[26];
    39 map<char,int>m;
    40 int main()
    41 {
    42     ios::sync_with_stdio(false);
    43     int n,nc;
    44     while(scanf("%d%d",&n,&nc)!=EOF){
    45     scanf("%s",s);
    46     int len=strlen(s);
    47     int b=0;
    48     memset(a,0,sizeof(a));
    49     for(int i=0;i<len;i++)
    50     {
    51         if(c[s[i]-'a']==0)c[s[i]-'a']=1;
    52     }
    53     int t=0;
    54     for(int i=0;i<26;i++)
    55     {
    56         if(c[i])
    57         {
    58             m[i+'a']=t;
    59             t++;
    60         }
    61     }
    62     b=1;
    63     int bl=0;
    64     //for(int i=0;i<len;i++)cout<<m[s[i]]<<endl;
    65     for(int i=0;i<n;i++)b=(b*nc)%B;
    66     for(int i=0;i<n;i++)
    67     {
    68         bl=((bl*nc)%B+m[s[i]])%B;
    69     }
    70     a[bl]=1;
    71     for(int i=n;i<len;i++)
    72     {
    73         bl=((bl*nc-m[s[i-n]]*b+B)%B+m[s[i]])%B;
    74         a[bl]=1;
    75     }
    76     int ans=0;
    77     for(int i=0;i<B;i++)
    78     {
    79         if(a[i])ans++;
    80     }
    81     printf("%d
    ",ans);
    82     }
    83     return 0;
    84 }
    代码君
  • 相关阅读:
    HDU2054_A == B ?【模拟题】【大数】【水的问题】
    hadoop结构出现后format变态
    jquery的clone办法bug修复
    Django学习笔记(三)—— 型号 model
    华夏的理财30天A和华夏财富宝货币哪个收益比较好?
    网贷平台公司 必看
    网贷平台 平安 陆金所
    网贷平台公司 各平台投资收费比较
    2012年网贷平台全国排名
    新进网贷的小散 感受
  • 原文地址:https://www.cnblogs.com/fraud/p/4338471.html
Copyright © 2011-2022 走看看