标签:poj 1200 crazy search hash many people like to
~~~~
果然用map暴力直接超时。原来要用hash,第一次写hash,真是个好玩的东西。
题目链接:http://poj.org/problem?id=1200
注意:1.题目说文本是由字符组成的,所以要用ascll码作为下标。
2.hash数组要开大点,否则RE。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #define N 16000000 using namespace std; int q[200]; int hash[N]; char str[N]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int val=-1,tot=0; scanf("%s",str); int len=strlen(str); memset(q,0,sizeof(q)); memset(hash,0,sizeof(hash)); //以ascll码为下标给每个字符赋值。 for(int i=0;i<len;i++) if(!q[str[i]]) q[str[i]]=++val; //枚举出每个长度为n的字串。 for(int i=0;i<=len-n;i++) { int s=0; for(int j=i;j<i+n;j++) s+=s*m+q[str[j]]; //映射hash函数。 if(!hash[s]) //至于为什么用类似进制的映射函数,还请大牛指导卖萌。 { tot++; hash[s]=1; } } printf("%d\n",tot); } return 0; }
POJ 1200 Crazy Search(hash).,布布扣,bubuko.com
标签:poj 1200 crazy search hash many people like to
原文地址:http://blog.csdn.net/darwin_/article/details/38579707