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(전재 표시)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
UVA 10025(수학)Given the following formula, one can set operators '+' or '-' instead of each '?', in order to obtain a given k ? ? n = ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.