poj 1200 crazy search Hash

10297 단어 search
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
    ",ans); return 0; }

    좋은 웹페이지 즐겨찾기