zoukankan      html  css  js  c++  java
  • POJ-1200 Crazy Search ( hash )

    题目链接: http://poj.org/problem?id=1200

    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

    求字符串中给定长度的不同子串的个数
    咋一看,NC似乎没什么用,但是这正是hash的关键:以NC为进制对子串取hash值。例如样例中,NC为4,取四进制,按读取顺序,d,a,b,c分别对应1,2,3,4然后依次去hash值即可。

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 
     5 int hash[16000005];
     6 int ex[128];
     7 string str;
     8 
     9 int main(){
    10     ios::sync_with_stdio( false );
    11 
    12     int n, nc;
    13     while( cin >> n >> nc ){
    14         memset( hash, 0, sizeof( hash ) );
    15         memset( ex, 0, sizeof( ex ) );
    16 
    17         cin >> str;
    18 
    19         int len = str.size();
    20         int num = 0, base = 1, temp = 0;
    21 
    22         for( int i = 0; i < len; i++ ){
    23             if( ex[str[i]] == 0 ) ex[str[i]] = ++temp;
    24             if( temp == nc ) break;
    25         }
    26 
    27         for( int i = 0; i < n; i++ ){
    28             num = num * nc + ex[str[i]] - 1;
    29             base *= nc;
    30         }
    31         base /= nc;
    32 
    33         hash[num] = 1;
    34 
    35         int ans = 1;
    36 
    37         for( int i =1; i <= len - n; i++ ){
    38             num = ( num - ( ex[str[i - 1]] - 1 ) * base ) * nc + ex[str[i + n - 1]] - 1;
    39             if( !hash[num] ){
    40                 hash[num] = 1;
    41                 ans++;
    42             }
    43         }
    44 
    45         cout << ans << endl;
    46     }
    47 
    48     return 0;
    49 }
  • 相关阅读:
    第一阶段-坑爹猴
    终于做出来了
    一天就制作成了这些
    累成狗做出来的
    一周的学习,组合起来的成就
    刚刚出炉的搜狗浏览器最新版本
    自己动手设计了一下下百度首页
    数论:卢卡斯定理(求组合数)
    数据结构:ST表模板(可维护区间RMQ)
    快读和快写(可以使用__int128)
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5384100.html
Copyright © 2011-2022 走看看