UVa 10282:babelfish

Source: Internet
Author: User
Tags hash

Topic Link:

Uva:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&category=24&page=show_ problem&problem=1223

poj:http://poj.org/problem?id=2503

Type: Hash table

Original title:

You are have just moved from Waterloo to a. The people here is speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input consists of up to 100,000 dictionary entries, followed from a blank line, followed through a message of the up to 100,000 words . Each dictionary entry was a line containing a Chinese word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message was a sequence of words in the foreign language and one word on each line. Each word in the "input is a" sequence of most lowercase letters. Output is the message translated to 中文版, one word per line. Foreign words not in the dictionary should to be translated as "eh".

Sample Input

Dog Ogday
cat Atcay
pig igpay
froot Ootfray
loops oopslay Atcay ittenkay
oopslay

Output for Sample Input

Cat
eh
loops

The main effect of the topic:

You are in a strange place, and the words in this place are different from the English words. Each English word corresponds to this one local word (all is one by one correspondence, does not appear repeatedly). Then you will give some local words to you to output the corresponding English words.

Analysis and Summary:

English words and local words are simple mapping relationships. There are 100,000 of the number given to the topic, with the hash set up mapping or direct sorting after the second search, even if it can be reluctantly passed, the speed should also be very unsatisfactory.

So the best way to do this is to use a hash table to create a mapping relationship.

The speed was good, and ran into rank 2nd.

* * UVA 10282-babelfish * Hash table * TIME:0.076S (UVA) * author:d_double/#include <iostream> #inclu    
de<cstdio> #include <cstring> #define MAXN 100003 using namespace std;  
      
      
typedef char WORD[12];  
Word ENGLISH[MAXN], FOREIGN[MAXN];  
const int hashsize = 100003;  
      
int N, head[hashsize], next[hashsize];    
    inline void init_lookup_table () {n=1;  
memset (head, 0, sizeof);  
    } inline int hash (char *str) {//string hash function int seed = 131;  
    int hash=0;  
    while (*str) hash = hash * seed + (*str++);  
Return (hash & 0x7fffffff)% Hashsize;  
    int Add_hash (int s) {int h = hash (foreign[s]);  
    int u = head[h];  
    while (u) u = next[u];  
    Next[s] = head[h];  
    HEAD[H] = s;  
return 1;  
    int search (char *s) {int h = hash (s);  
    int u = head[h];  
        while (U) {if (strcmp (Foreign[u], s) ==0) return u;  
    U = next[u];  }
    return-1;  
    int main () {char str[25];  
    N = 1;  
    Init_lookup_table ();  
        while (gets (str)) {if (str[0]== ' ") break;   
        int i;  
        for (i=0; str[i]!= '; ++i) english[n][i] = Str[i];  
        English[n][i] = ' the ';   
        Char *p=str+i+1;   
        i=0;  
        while (*p) foreign[n][i++] = *p++;  
        Foreign[n][i]= ' ";  
        Add_hash (N);  
    ++n;  
        //Query while (gets (str)) {int index = search (str);  
        if (Index==-1) puts ("eh");  
    else printf ("%s\n", English[index]);  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.