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

APHASH 188ms

Posted by ZxyElf at 2013-01-30 22:48:46 on Problem 2503
#include <iostream>
#include <cstring>
using namespace std;
struct node{
	char trans[11];
	char ori[11];
	node *next;
};

node head[99992];
node heap[100000];

int cursor;

unsigned int APHash(char *str)
{
    unsigned int hash = 0;
    int i;
 
    for (i=0; *str; i++)
    {
        if ((i & 1) == 0)
        {
            hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));
        }
        else 
        {
            hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));
        }
    }
 
    return (hash & 0x7FFFFFFF)%99991;
}

int main(void)
{
	char buf[30];
	int i=0,j;
	while (gets(buf) && buf[0]){
		char *itr=buf;
		j=0;
		while (*itr!=' ')
			heap[i].trans[j++]=*itr++;	
		heap[i].trans[j]=0;
		itr++;j=0;
		while (*itr)
			heap[i].ori[j++]=*itr++;
		heap[i].ori[j]=0;
		int h=APHash(heap[i].ori);
		if (head[h].next){
			node *n=head[h].next;
			while (n->next)
				n=n->next;
			n->next=&heap[i];
		}else
			head[h].next=&heap[i];
		i++;
	}
	
	while (gets(buf)){
		int h=APHash(buf);
		node *n=head[h].next;
		while (n && strcmp(n->ori,buf))
			n=n->next;
		if (n)
			printf("%s\n",n->trans);
		else
			printf("eh\n");
	}

	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