Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请问这个题目hash怎么这么慢呢??????

Posted by ACM_henry at 2008-10-23 21:03:21 on Problem 2945
#include <iostream>
using namespace std;
char p[20010][30];
int nums[20010];
const int prim=100001;
struct h
{
	int key;
	int num;	
}hash[prim];
void insert(long v)
{
    long key=v; while(key<0) key+=prim;
    if(key>=prim) key=key%prim;
    while(hash[key].num!=0)
	{
         if(hash[key].key==v){ hash[key].num++; return; }
         key=(key+1)%prim;            
    }
    hash[key].num=1; hash[key].key=v;
}
int search(long v)
{
    long key=v; while(key<0) key+=prim;
    if(key>=prim) key=key%prim;
    while(hash[key].num!=0)
	{
         if(hash[key].key==v) return hash[key].num;
         key=(key+1)%prim;                      
    }
    return 0;
}
int main()
{
	int sum;
	int i,j;
	int hang,lie;
	while(scanf("%d%d",&hang,&lie)==2)
	{
	if(hang==0&&lie==0)
	{
		break;
	}
	for(i=0;i<prim;i++)
	{
		hash[i].num=0;

	}
	memset(nums,0,sizeof(nums));
	int aim;
	for(i=0;i<hang;i++)
	{
		sum=0;
		scanf("%s",p[i]);
		for(j=0;j<strlen(p[i]);j++)
		{
			switch(p[i][j])
			{
			case 'C':
				sum+=sum*4+1;
				break;
			case 'A':
				sum+=sum*4+2;
				break;
			case 'T':
				sum+=sum*4+3;
				break;
			case 'G':
				sum+=sum*4+4;
				break;
			}
		}
		insert(sum);
		aim=search(sum);
		nums[aim]++;
		nums[aim-1]--;
	}
	for(i=1;i<=hang;i++)
	{
		printf("%d\n",nums[i]);
	}	
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator