| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:求测试数据In Reply To:Re:求测试数据 Posted by:sths at 2012-04-25 21:33:03 > 发下代码。。我帮你看看。。
刚才忘了给注释,你看这一个吧。。。GOOD LUCK!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#define max 100100
#define mod 99991
typedef struct HASH
{
char f[12],e[12];//f[],e[]分别是外来语、英语
struct HASH *next;
HASH()
{
next=NULL;
}
}hashtable;
hashtable *Hash[mod];
void insert(char *s1,char *s2)
{
int key=0,i;
for(i=0;*s2;i++)
key+=*(s2++)*i;
key%=mod; //以上是获得Key值 //一下便是判重、以及插入
if(!Hash[key]) //KEY值没有冲突
{
hashtable* pn=new hashtable;
strcpy(pn->e,s1);
strcpy(pn->f,s2);
Hash[key]=pn;
}
else //key值有冲突
{
hashtable* pn=Hash[key];
while(pn->next)
pn=pn->next;
hashtable *temp=new hashtable;
strcpy(temp->e,s1);
strcpy(temp->f,s2);
pn->next=temp;
}
}
void find(char *s1) //根据输入的外来语,输出相应的英语
{
int key=0,i;
for(i=0;*s1;i++)
key+=*(s1++)*i;
key%=mod;
if(!Hash[key]) //没有该外来语的key
{
printf("eh\n");
return ;
}
else //key冲突
{
hashtable *temp=Hash[key];
while(temp)
{
if(!strcmp(temp->f,s1))
{
printf("%s\n",temp->e);return ;
}
temp=temp->next;
}
printf("eh\n"); return ;//key冲突但是语言不同
}
}
int main()
{
freopen("in.txt","r",stdin);
char s1[12],s2[12];
memset(Hash,0,sizeof(Hash));
while(scanf("%c",&s1[0]),s1[0]!='\n')
{
scanf("%s%s",s1+1,s2);
insert(s1,s2);
getchar();
}
while(scanf("%s",s1)==1)
{
find(s1);
getchar();
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator