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为什么会WA呢?? 哪位兄弟给个我过不了的数据?难道是冲突问题?

Posted by ACM_henry at 2008-11-10 13:19:31 and last updated at 2008-11-10 13:27:42
In Reply To:Re:用map就行,注意句子开头不是字母的情况 Posted by:dalonghenin at 2008-11-09 23:11:36
#include <iostream>
#include "string"
#include <algorithm>
using namespace std;
const int prim=1000001;
char aim[10002][30];
struct h
{
	int key;
	int num;	
	int index;
	char a,b;
}hash[prim];
char p[100000];
void insert(long v,int x,char a,char b)
{
    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].a==a&&hash[key].b==b)
		 { 
			 hash[key].num++;
			 hash[key].index=x;
			 return;
		 }
         key=(key+1)%prim;            
    }     
    hash[key].num=1; hash[key].key=v;
	hash[key].index=x;
	hash[key].a=a;hash[key].b=b;
}

int search(long v,char a,char b)
{
    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].a==a&&hash[key].b==b) 
			 return hash[key].index;
         key=(key+1)%prim;                      
    }
    return -1;
}

int main()
{
	int sum;
	char q[30];
	int i,j,index=0;
	for(i=0;i<prim;i++)
	{
		hash[i].num=0;
	}
	while(gets(p))
	{
		j=0;
		if(strcmp(p,"DICTIONARY_DEFINE_OVER")==0)
			break;
		for(i=0;i<strlen(p);i++)
		{
			if(p[i]==' ')
			{
				aim[index++][j]='\0';
				j=0;
			}
			else 
			{
				aim[index][j++]=p[i];
			}
		}
		aim[index++][j]='\0';
	}
	int k;
	for(i=0;i<index;i++)
	{
		for(j=0;j<strlen(aim[i]);j++)
		{
			q[j]=aim[i][j];
		}
		q[j]='\0';
		sort(q,q+j);
		sum=0;
		for(k=0;k<j;k++)
		{
			sum=sum*26+q[k]-'a'+1;
			sum%=prim;
		}
		insert(sum,i,aim[i][0],aim[i][j-1]);
	}
	string s;
	while(gets(p))
	{
		char a,b;
		j=0;
		for(i=0;i<strlen(p);i++)
		{
			if(p[i]<'a'||p[i]>'z')
			{
				if(j!=0)
				{
					a=q[0];
					b=q[j-1];
					q[j]='\0';
					s.assign(q);
					sort(q,q+j);
					sum=0;
					for(k=0;k<j;k++)
					{
						sum=sum*26+q[k]-'a'+1;
						sum%=prim;
					}
					int aa=search(sum,a,b);
					if(aa!=-1)
					{
						cout<<aim[aa];
					}
					else cout<<s;
				}
				cout<<p[i];
				j=0;
			}
			else 
			{
				q[j++]=p[i];
			}
		}
		if(j!=0)
		{
					a=q[0];
					b=q[j-1];
					q[j]='\0';
					s.assign(q);
					sort(q,q+j);
					sum=0;
					for(k=0;k<j;k++)
					{
						sum=sum*26+q[k]-'a'+1;
						sum%=prim;
					}
					int aa=search(sum,a,b);
					if(aa!=-1)
					{
						cout<<aim[aa];
					}
					else cout<<s;
		}
		cout<<endl;
	}
	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