Poj 2503 Babelfish(map+串的处理)

来源:互联网 发布:帝国cms图片自动加alt 编辑:程序博客网 时间:2024/06/05 17:28
Babelfish
Time Limit: 3000MS Memory Limit: 65536KTotal 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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops



先声明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
原创粉丝点击