zoukankan      html  css  js  c++  java
  • [HDOJ1381]Crazy Search

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1381

    Crazy Search

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2236    Accepted Submission(s): 827


    Problem 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.
    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is 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
    1 3 4 daababac
     
    Sample Output
    5
     
    hash,给每一个字符映射成一位nc进制的数,再给每一个子串算一个value(秦九韶算法),检查是否出现过,出现过就不统计了。貌似STL可以直接做
     
     
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 16000010;
    16 
    17 bool has[maxn];
    18 char str[maxn];
    19 int ascii[256];
    20 int n, nc;
    21 
    22 int solve() {
    23     memset(has, 0, sizeof(has));
    24     memset(ascii, 0, sizeof(ascii));
    25     int cnt = 1;
    26     for(int i = 0; str[i]; i++) {
    27         if(!ascii[str[i]]) {
    28             ascii[str[i]] = cnt;
    29             cnt++;
    30         }
    31         if(cnt == nc) {
    32             break;
    33         }
    34     }
    35     int len = strlen(str) - n;
    36     int ans = 0;
    37     for(int i = 0; i <= len; i++) {
    38         int val = 0;
    39         for(int j = 0; j < n; j++) {
    40             val = val * nc + ascii[str[i+j]];
    41         }
    42         if(!has[val]) {
    43             has[val] = 1;
    44             ans++;
    45         }
    46     }
    47     return ans;
    48 }
    49 
    50 int main() {
    51     int T;
    52     scanf("%d", &T);
    53     while(T--) {
    54         scanf("%d %d", &n, &nc);
    55         getchar();
    56         scanf("%s", str);
    57         printf("%d
    ", solve());
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    SAP HANA概述——SAP HANA学习笔记系列
    如何在Visual Studio 2010中使用CppUTest建立TDD的Code Kata的环境
    客户旅程分析 Customer Journey Mapping
    “用户故事”来做展会——敏捷嘉年华(敏捷之旅2012上海站)经验分享
    相机翻拍书本
    外网访问路由
    新编全医药学大词典下载地址
    MKV声道切换花屏处理
    几何画板动态表达式(文本与变量合并)
    修改IP地址
  • 原文地址:https://www.cnblogs.com/kirai/p/4755475.html
Copyright © 2011-2022 走看看