zoukankan      html  css  js  c++  java
  • HDU_1381 Crazy Search

    Crazy Search

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


    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
     


    Recommend
    Eddy
     

      

           这道题思路很简单,就是先给字符串的的字符从[1...NC]分配一个值,再将字符串转成NC进制,最后用hash表得出结果,关键是杭电这题的数据太变态了!如果用c语言写,hash数组开到16000000,就会MLE,如果开小了就会RE。而c++里使用bool型定义hash数组就能AC(C99里没有bool型数据,如果在c语言里用bool型, 我用的GCC编译器根本无法编译!),最后只能用C++写。还有就是,这题描述的太让人费解, 好多条件都是我交了好几次试出来的!

    代码:

    #include <cstdio>
    #include
    <cstring>
    using namespace std;
    char str[1000000];
    int ascii[125];
    bool hash[16000000]; //这里使用bool型定义,因为bool型数据在内存中占一个字节,而int型占两个
    int main()
    {
    int t, n, m, j;
    //freopen("data.in", "r", stdin);
    scanf("%d", &t);
    while(t--)
    {
    scanf(
    "%d%d", &n, &m);
    scanf(
    "%s", str);
    int len = strlen(str);
    int k = 0, num = 0, i = 0;
    memset(hash,
    0, sizeof(hash));
    memset(ascii,
    0, sizeof(ascii));
    while(k != m)
    {
    if(!ascii[str[i]-'A']) ascii[str[i]-'A'] = k++;
    i
    ++;
    }
    for(i = 0; i <= len-n; i++)
    {
    int sum = 0;
    for(j = 0; j < n; j++)
    {
    sum
    = sum*m + ascii[str[j+i]-'A'];
    }
    if(!hash[sum])
    {
    hash[sum]
    = true; num++;
    }
    }
    printf(
    "%d\n", num);
    if(t) printf("\n");
    }
    return 0;
    }
  • 相关阅读:
    自动关联
    如何提高测试效率
    检查点
    windows server 2008 安装vs2008 的问题
    【转】xampp mysql 忘记密码的解决方案
    zencart 目录产品显示控制
    静态html文件执行php语句的方法
    UNIX主机访问PHP程序提示“Internal Server Error”的处理办法
    【转】javascript 点击 <a> 链接
    u880刷机
  • 原文地址:https://www.cnblogs.com/vongang/p/2130281.html
Copyright © 2011-2022 走看看