poj 2002 Babelfish

算法:

1.map 985ms..

2.Trie树,250ms..4倍。。

3.排序+二分查找 200ms左右

struct dict
{
    char a[11];
    char b[11];
}dic[100001];

4. HASH

将字符串映射为一个HASH值,

HASH函数可以从网上自己找一个

用静态链表处理冲突,速度最快235ms

View Code
/*
代码自己没写了,此代码来自
http://wingszero.blogbus.com/logs/60733344.html
*/

#include<iostream>
#include<string>
using namespace std;
const int maxn=100001;
const int maxl=11;
const int prime=99997;
char a[maxl],b[maxl],w[maxl*2];
int pos=0;

int Hash(char*str)
{
    unsigned int b=378551 ;
    unsigned int a=63689 ;
    unsigned int hash=0 ;
     
    while(*str)
    {
        hash=hash*a+(*str++);
        a*=b ;
    }
     
    return(hash % prime);
} 
int head[prime];
struct Edge
{
  char a[maxl],b[maxl];
  int next;
}edge[maxn];
int num;
void init()
{
  num=1;
  memset(head,0,sizeof(head));
}
void find(char a[])
{
  int hash=Hash(a);
  for(int i=head[hash];i;i=edge[i].next)
  {
  if(strcmp(a,edge[i].a)==0)
  { 
   printf("%s\n",edge[i].b);
   return;
  }
}
  printf("eh\n");
}
void add(char a[],char b[])
{
  int hash=Hash(b);
  edge[num].next=head[hash];
  strcpy(edge[num].a,b);
  strcpy(edge[num].b,a);
  head[hash]=num++;
}
int main()
{
  init();
  while(gets(w) && w[0]!=0)
  {
    sscanf(w,"%s %s",a,b);
    add(a,b);
  }
  char tmp[maxl];
  while(scanf("%s",tmp)!=EOF)
  {
    find(tmp);
  }
}

 

 

View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;

#define MAXN 260000 
struct TrieNode
{
  char word[12];
  TrieNode *next[27];
}root;

TrieNode node[MAXN];
int n = 0;

void insert(char *a, char *b)
{
   if( a == NULL )
      return;
   TrieNode *p = &root;
   while( *a )
   {
       if( p->next[*a - 'a'] == NULL )      
          p->next[*a - 'a'] = &node[n++];
       p = p->next[*a - 'a'];
       a++;   
   }
   strcpy(p->word, b);
}

void search(char *a, char *b)
{
   if( a == NULL )
      return ;
   TrieNode *p = &root;
   while( *a )
   {
       if( p->next[*a - 'a'] )
       {      
           p = p->next[*a - 'a'];
       }
       else
       {
          strcpy(b, "eh");
          return;    
       }
       a++;              
   }
   strcpy(b, p->word);   
}

int main( )
{
  int N;
  n = 0;
  char a[100], b[100];
  while( scanf("%s%s",a,b) != EOF )
  {
     insert(b, a);
     getchar( );
     char ch;
     ch = getchar();
     if( ch != '\n' )
     {  
         ungetc(ch,stdin); //好东西,把字符退回到输入流当中 
         continue;    
     }
     break;         
  }
  while( scanf("%s",a) != EOF)
  {
     search(a,b);
     printf("%s\n",b);       
  }
    
    
}

 

View Code
#include<iostream>
#include<map>
#include<string>
using namespace std;

map<string,string>mp;

int main( )
{
   string st, a, b;
   int n;
   mp.clear( );
   while( getline(cin,st) )
   {
     if((n = st.find(' ')) != string::npos )
     {
        int len = st.length( );
        a = st.substr(0, n);
        b = st.substr(n+1,len);
        mp[b] = a;    
     }
     else
     {
        if( st == "" ) continue; //此处注意哈,写成 st == " ",一直错。。 
        if( mp[st] != "" ) cout<<mp[st]<<endl;
        else cout<<"eh"<<endl;    
         
     }      
          
          
   }    
   return 0;
}

posted on 2012-07-21 16:28  more think, more gains  阅读(146)  评论(0编辑  收藏  举报

导航