UVa 10282 - Babelfish

3770 단어 uva
제목 링크:
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
형식:해시 테이블
원제:
You have just moved from Waterloo to a big city. The people here 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 by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters. Output is the message translated to English, one word per line. Foreign words not in the dictionary should 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

제목 의 대의:
너 는 낯 선 곳 에 도착 했다.이곳 의 단 어 는 영어 단어 와 다르다.모든 영어 단 어 는 이 현지의 단어 에 대응한다.그리고 해당 하 는 영어 단 어 를 출력 하 라 는 현지 단 어 를 제시 할 것 이다.
분석 과 총 결:
영어 단어 와 현지 단 어 는 간단 한 매 핑 관계 이다.제목 이 주 는 수량 이 100,000 에 달 하고 hash 로 맵 을 만 들 거나 직접 정렬 한 후 2 분 동안 찾 을 수 있 습 니 다.겨우 통과 할 수 있 더 라 도 속도 가 이상 적 이지 않 을 것 입 니 다.
그래서 이 문제 의 가장 좋 은 방법 은 해시 표 로 매 핑 관 계 를 맺 는 것 이다.
스피드 가 괜찮아 랭 킹 2 위 에 올 랐 어 요大笑
/*
 * Uva 10282 - Babelfish
 *    
 * Time: 0.076s (UVa)
 * Author: D_Double
 */
#include<iostream>  
#include<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(head));
}

inline int hash(char *str){ //        
    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]=='\0') break;
        int i; 
        for(i=0; str[i]!=' '; ++i)
            english[N][i] = str[i];
        english[N][i] = '\0';
        char *p=str+i+1; 
        i=0; 
        while(*p) foreign[N][i++] = *p++;
        foreign[N][i]='\0';
        add_hash(N);
        ++N;
    }
    //   
    while(gets(str)){
        int index = search(str);
        if(index==-1) puts("eh");
        else printf("%s
", english[index]); } return 0; }

-생명의 의 미 는 그 에 게 의 미 를 부여 하 는 데 있다.
오리지널http://blog.csdn.net/shuangde800,By DDouble(전재 표시)

좋은 웹페이지 즐겨찾기