POJ2503 Babelfish

题目大意:给出英语 --- A国语言 ,然后输入A国语言单词,问在字典内是否有该单词,有就输出该单词英文,无则输出eh;

思路:典型的先保存再查找,有好几种解法,我用得是hash,但是再用链表的时候无限WA啊,也不知道是那个字符串哈希有问题还是链表出了问题,求强力数据找BUG啊

 


AC program:(数组哈希)

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
char aa[15],bb[15];
char cc[15];
char w[24];
int num;
int head[100005];
struct node
{
  char str1[15],str2[15];
  int tto;
         
}next[100005];
int get_key(char tmp[])
{
    unsigned int key=0;
    unsigned int a=61891;  //unsigned保证数据不爆
    while(*tmp)
    {
      key=key*a+(*tmp++);           
    }
    return key%99991;
}
void hash_insert(char aaa[],char bbb[])
{
  int key=get_key(bbb);
  next[num].tto=head[key];
  strcpy(next[num].str1,aaa);
  strcpy(next[num].str2,bbb);///
  head[key]=num++;
    
}
void hash_find(char ccc[])
{
  int key=get_key(ccc);
  for(int i=head[key];i;i=next[i].tto)
  {
    if(strcmp(next[i].str2,ccc)==0)
      {
        printf("%s\n",next[i].str1);
        return;
      }      
  }
  printf("eh\n");     
}
int main()
{
num=1;///
memset(head,0,sizeof(head));
memset(next,0,sizeof(next));
while(gets(w) && w[0])
{
  sscanf(w ,"%s%s",aa,bb);
  hash_insert(aa,bb);              
}
while(scanf("%s",cc)!=EOF)
{ 
  hash_find(cc);                          
}
//system("pause");
return 0;}


 

 


WA program:(链表哈希,呜呜,各种WA,求数据啊)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define HashTableSize 99991 
char aa[20],bb[20];
char cc[20];
char w[40];
typedef class HashTable
{
  public :
          char str1[20],str2[20];
          HashTable *next;
          HashTable()
          {
               next=0;
               memset(str1,0,sizeof(str1));
               memset(str2,0,sizeof(str2));///  
          }
         
}Hashtable;
 
Hashtable * hash[100005];
/*unsigned int ELFHash (const char *str)
{
  unsigned int key=0;
  unsigned int x=0;
  while(*str)
  {
    key=(key<<4)+(*str++);
    if((x=key&0xF0000000L)!=0)
     {
       key^=(x>>24);
       key&=~x;                         
     }           
  } 
  return key;//key&0x7fffffff;        
}*/
int ELFHash(char * key)
{
    unsigned int h = 0;
    while (*key){
        h = (h << 4) + *key++;
        unsigned int g = h & 0xf0000000L;
        if (g) h ^= g >> 24;
        h &= ~g;
    }
    return h;//h % 99991;
} 
void hash_insert(char aaa[],char bbb[])
{
    int key=ELFHash(bbb)%HashTableSize;
    if(!hash[key])
      {
       Hashtable *temp =new   Hashtable ; 
       //temp->str1=aaa; 
       strcpy(temp->str1,aaa);
       strcpy(temp->str2,bbb);
       hash[key]=temp;           
      }
    else
       {
                Hashtable *temp=hash[key];
                while(temp->next)
                   temp=temp->next;
                temp->next=new  HashTable;
                strcpy(temp->str1,aaa);
                strcpy(temp->str2,bbb);                          
       } 
       
} 
bool hash_find(char ccc[])
{
 int key=ELFHash(ccc)%HashTableSize;
 if(!hash[key])
    return false;
 else
     {
              Hashtable *temp=hash[key];
              while(temp)
              {
                if(strcmp(temp->str2,ccc)==0)
                {
                  printf("%s\n",temp->str1);
                  return true;                             
                } 
                temp=temp->next;          
              }
                    
     }
     return false;     
} 
int main()
{
memset(hash,0,sizeof(hash));  
/*while(1)
      {
           
            if((aa[0]=getchar())=='\n')break;
            scanf("%s%s",aa+1,bb);
            insert(aa,bb);
            //getchar();
      }*/
 
while(gets(w) && w[0]!=0)
  {
    sscanf(w,"%s %s",aa,bb);
    hash_insert(aa,bb);//add(a,b);
  } 
while(scanf("%s",cc)!=EOF)/// 
{
   if(!hash_find(cc))
     printf("eh\n");                         
} 

                                   
return 0;} 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值