poj 1200 crazy search Hash

Problem: 1200 User: sunyanfei
Memory: 30904K Time: 63MS
Language: G++ Result: Accepted
    • Source Code
/*

 * Author:lonelycatcher

 * Problem:poj 1200 crazy search

 * Type:Hash

 */

#include <iostream>

#include<string>

#include<string.h>

#include<stdio.h>

using namespace std;

char str[1000000];

int hash[16000000];

int ansi[256]={0};

int main()

{

	int N,NC,ans=0,i,j;

	setbuf(stdout,NULL);

	scanf("%d%d%s",&N,&NC,str);

	int len=strlen(str);

	for(i=0;i<len;i++)

	{

		ansi[(int)str[i]]=1;

	}

	int cnt=0;

	for(i=0;i<256;i++)

	{

		if(ansi[i])

			ansi[i]=cnt++;

	}

	for(i=0;i<=len-N;i++)

	{

        int key=0;

        for(j=0;j<N;j++)

        {

        	key=key*NC+ansi[(int)str[i+j]];//转换成NC进制

        }

        if(!hash[key])

        {

        	ans++;

        	hash[key]=1;

        }

	}

	printf("%d\n",ans);

	return 0;

}

你可能感兴趣的:(search)