poj 1200 Crazy Search

题目:

链接:点击打开链接

题意:

输入n和nc,以及字符串s,输出长度为n的不同字串的个数。

算法:

思路:

用hash判重(hash值。。。。。。),看了大牛的代码,对hash还是不甚理解。。。。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdio>
using namespace std;
#define MAXN 16000010
const int MAXN1 = 257;

int hash[MAXN];
int m[MAXN1];
char s[1000000];

int main()
{
    freopen("input.txt","r",stdin);
    int n,nc;
    int sum = 0,sum1 = 0,sum2 = 0;
    memset(hash,0,sizeof(hash));
    memset(m,0,sizeof(m));
    memset(s,0,sizeof(s));
    scanf("%d%d",&n,&nc);
    getchar();
    scanf("%s",s);
    int len = strlen(s);
    for(int i=0; i<len; i++)
    {
        if(!m[s[i]])//将每个字符赋值为相应ASCII的数
        {
            m[s[i]] = ++sum;
        }
        if(sum == nc)
        {
            break;
        }
    }
    for(int i=0; i<=len-n; i++)
    {
        sum1 = 0;
        for(int j=0; j<n; j++)//将长度为n的子串变为一个nc进制的整数
        {
            sum1 = sum1*nc + m[s[i+j]]-1;
        }
        if(!hash[sum1])//如果hash[sum1]==0表示没有没有出现过
        {
            hash[sum1] = 1;//标记
            ++sum2;//不同串的个数加1
        }
    }
    printf("%d\n",sum2);
    return 0;
}

poj 1200 Crazy Search,布布扣,bubuko.com

时间: 2024-12-24 06:47:37

poj 1200 Crazy Search的相关文章

POJ 1200 Crazy Search(Hash)

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 t

POJ 1200 Crazy Search (字符串hash)

题目大意: 分析长度为n的子串有多少种. 思路分析: 对于没出现的字符,将其分配一个数字. 然后将子串看做一个nc进制的数. 然后hash判断. #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <string> using namespace std; bool vis[26666

Poj 1200 Crazy Search(字符串Hash)

Poj 1200 题意:给你一个n和m以及一个有m个不同字母组成的字符串,问有多少个长度为n的不同字符子串: 题解:以m为进制进行Hash.虽然是看了解题报告才会的但必须要理解并且学会运用:https://www.cnblogs.com/gj-Acit/archive/2013/05/15/3080734.html. #include<cstring>//别用set,set的添加是红黑树实现时间复杂度是O(logN),在这里会超时 #include<cstdio> #define

Crazy Search

poj1200:http://poj.org/problem?id=1200 题意:给你一个有m种字符串,求长度为n的连续子串由多少种. 题解:网上的代码都是hash,但是本人觉得hash有问题,就是n,m稍微大点,hash的值都会爆出int,无法开数组来记录该串是否被记录.可能数据弱,结果hash竟然A了 .正确的解法是用set来存,把所有的hash值放进set,set有去重效果,最后求一下set的大小.但是这样结果是T了.不知道这题怎么解.一下是第一种代码.于是换别的,trie树来搞. Cr

POJ-1200 Crazy Search(hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27336   Accepted: 7641 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 gi

[poj1200]Crazy Search(hash)

Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26713 Accepted: 7449 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

POJ 1785 Binary Search Heap Construction (线段树)

题目大意: 给出的东西要求建立一个堆,使得后面的数字满足堆的性质,而且字符串满足搜索序 思路分析: 用线段树的最大询问建树.在建树之前先排序,然后用中序遍历递归输出. 注意输入的时候的技巧... #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define lson num<<1,s,mid #define rson num<<

poj 1200 (hash)

Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted: 6513 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 gi

poj 1200 --- 不错的字符串HASH构造方法

题目:http://poj.org/problem?id=1200 题意:给一个字符串,给定n和nc,字符串里最多有nc个不同的字符,问长度为n的不同子串最多有几个 和上一篇现场赛那个一样,也是难在判重处理不好会超时 方法:将长度为n的子串映射为一个nc进制的数,开一个大数组,判断是否重复 #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include