Poj 2503 Babelfish(map+串的处理)

11 篇文章 0 订阅
6 篇文章 0 订阅
Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 36520 Accepted: 15587

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



先声明G++WA ,C++ AC。。


一种应射的关系,所以可以用map来做。主要是输入问题。解囧方法是gets()+sscanf()


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
char e[18],f[18],f2[18],tmp[18],now[18];
using namespace std;
int main()
{
    int n,i,m;
    map<string,bool>is;
    map<string,string>tr;
    ios::sync_with_stdio(false);
    while(gets(tmp)!=NULL)
    {
        if(!strcmp(tmp,""))//由题目给定的输入可以看出,在什么都没有的情况下进行,外语的输入。
            break;
            cout<<endl;
        sscanf(tmp,"%s %s",e,f);//如果这里输出tmp 的值  ,则是 e+中间有的空格+f。
        is[f]=true;
        tr[f]=e;
    }
    while(gets(f2)!=NULL)
    {
        if(!strcmp(f2,""))//""值什么都没有即回车,这里的意义和上边!strcmp(tmp,"")break;不同这里指的是前边没有输入的情况下
        continue;
        if(is[f2]==true)
            cout<<tr[f2]<<endl;
        else
            puts("eh");
    }

    return 0;
}

上边的sscanf()必须和gets()一起用,若无gets()sscanf();不会进行输入。






此外字典树一可以做:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stu
{
    struct stu *next[26];           
    char s[12];
}node;
node* creat_node()           //创建新节点并初始化
{
    node *p=(node *)malloc(sizeof(node));
    memset(p->next,0,sizeof(p->next));
    return p;
}
void trie_insert(node *p,char *s,char *word)
{
    int i;
    while(*s!='\0'){
        i=*s-'a';
        if(p->next[i]==0)
            p->next[i]=creat_node();
        p=p->next[i];
        s++;
    }
    strcpy(p->s,word);         //将对应单词存起来
}
void trie_search(node *p,char *s)
{
    int i;
    while(*s!='\0'){
        i=*s-'a';
        p=p->next[i];
        if(p==0){
            printf("eh\n");
            return ;
        }
        s++;
    }
    printf("%s\n",p->s);
}
int main()
{
    node *root=NULL;
    char s[25],t[12],word[12];
    root=creat_node();
    while(gets(s)!=NULL){
        if(strcmp(s,"")==0)
            break;
        sscanf(s,"%s %s",word,t);
        trie_insert(root,t,word);
    }
    while(gets(s)!=NULL)
        trie_search(root,s);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值