POJ 2503 Babelfish qsort+bserach OR 字典树

 

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36951 Accepted: 15743

Description

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

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

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

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

Source

acCODE:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
using namespace std;
struct Node{
    char word[20];
    char map[20];
}me[200000];
int cmp(const void *a,const void *b){
    return strcmp(((Node*)a)->map,((Node*)b)->map);
};
int bcmp(const void *a,const void *b){
    return strcmp(((char*)a),((Node*)b)->map);
};
int main(){
    char s[60];
    int n=0;
    Node *pos;
    while(gets(s)&&s[0]!='\0'){
        sscanf(s,"%s %s",me[n].word,me[n].map);
        n++;
    }
    qsort(me,n,sizeof(me[0]),cmp);
    while(gets(s)&&s[0]!='\0'){
        pos=(Node*)bsearch(s,me,n,sizeof(me[0]),bcmp);
        if(pos)printf("%s\n",pos->word);
        else printf("eh\n");
    }
    return 0;
}

暑假做的时候用二分现在用字典树在做一遍练练数据结构

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define MAX 27
using namespace std;
struct Trie{
    Trie *next[MAX];
    char tt[22];
    int v;
};
bool flag;
Trie *root;
void createTrie(char *str,char *t){
    int len=strlen(str);
    Trie *p=root,*q;
    FOR(i,0,len-1){
        int id=str[i]-'a';
        if(p->next[id]==NULL){
            q=(Trie *)malloc(sizeof(Trie));
            q->v=1;
            FOR(j,0,MAX-1)
                q->next[j]=NULL;
            p->next[id]=q;
            p=p->next[id];
        }
        else
            p=p->next[id];
    }
    p->v=10;
    strcpy(p->tt,t);
}
int find_Trie(char *str){
    int len=strlen(str);
    Trie *p=root;
    FOR(i,0,len-1){
        int id=str[i]-'a';
        p=p->next[id];
        if(p==NULL)
            return 0;
    }
    if(p->v==10)
    printf("%s\n",p->tt);
    return 1;
}
char str[33],s1[16],s2[16];
int main(){
    root=(Trie *)malloc(sizeof(Trie));
    FOR(i,0,MAX-1)
        root->next[i]=NULL;
    while(gets(str)&&str[0]!='\0'){
        sscanf(str,"%s %s",s2,s1);
        createTrie(s1,s2);
    }
    while(rds(str)!=EOF){
        flag=find_Trie(str);
        printf(!flag?"eh\n":"");
    }
    return 0;
}
/*
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值