POJ – 1200 Crazy Search

#include<iostream>
#include<cstring>
using namespace std;
const int maxn=2e7;   //这个2e7怎么来的???
int len,base,base1=0,myhash[300],ans=0;
bool vis[17001005];
char s[maxn];
int main(){
    memset(vis,0,sizeof(bool)*16000005);
    scanf("%d%d%s",&len,&base,s);
    int I=strlen(s);          //一开始没写I,竟然超时了???strlen()很耗时吗?
    for(int i=0;i<I;i++)
        if(!myhash[s[i]])    
            myhash[s[i]]=base1++;    //不能hash成0,不然后边算sum会有问题
    for(int i=0;i<=I-len;i++){
        int sum=0;
        for(int j=0;j<len;j++)
            sum=(sum*base1+myhash[s[i+j]]);
        if(!vis[sum]){
            vis[sum]=1;
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;}

猜你喜欢

转载自www.cnblogs.com/spzeno/p/10527775.html