http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109

Language of FatMouse


Time Limit: 10 Seconds      Memory Limit: 32768 KB

We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.

Input Specification

Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output Specification

Output is the message translated to English, one word per line. FatMouse 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

题意:fat有一种自己的语言,所以有自己的词典,每个fat的单词对应一个人类单词。输入是个词典。然后给你一系列fat的单词,通过词典给出对应的人类单词,如果找不到,输出eh

思路A:直接利用map映射。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define MAX 0x7fffffff
#define N 20 map<string,string> m;
map<string,string>::iterator it; int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
char s1[N],s2[N];
while(cin.peek()!='\n'){
scanf("%s %s",s1,s2);
getchar();
m.insert(pair<string,string>(string(s2),string(s1)));
}
getchar();
while(gets(s1)!=NULL){
it=m.find(s1);
if(it!=m.end()){
printf("%s\n",(*it).second.c_str());
}else{
printf("eh\n");
}
}
return ;
}

思路B:使用字典树,在node里另加个字符串,用来存储映射的字符串即可。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define MAX 0x7fffffff
#define N 20 struct node{
node *word[];
char* eng_word;
node(){
for(int i=;i<;i++) word[i]=NULL;
eng_word=NULL;
}
}*root; void Insert(char* s,char* st);
char* Find(char* s); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
char s1[N],s2[N];
root=new node;
while(cin.peek()!='\n'){
scanf("%s %s",s1,s2);
getchar();
Insert(s2,s1);
}
getchar();
while(gets(s1)!=NULL){
printf("%s\n",Find(s1));
}
return ;
}
void Insert(char* s,char* st){
int len=strlen(s);
node *current=root,*new_node;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL) current=current->word[s[i]-'a'];
else{
new_node=new node;
current->word[s[i]-'a']=new_node;
current=current->word[s[i]-'a'];
}
}
current->eng_word=new char[strlen(st)+];
strcpy(current->eng_word,st);
}
char* Find(char* s){
int len=strlen(s);
node *current=root;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL) current=current->word[s[i]-'a'];
else return "eh";
}
if(current->eng_word==NULL) return "eh";
else return current->eng_word;
}

zoj1109-Language of FatMouse 【字典树】的更多相关文章

  1. zoj 1109 Language of FatMouse(字典树)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  2. zoj 1109 zoj 1109 Language of FatMouse(字典树)

    好开心,手动自己按照字典树的思想用c写了一个优化后的trie字典树,就是用链表来代替26个大小的字符数组.完全按照自己按照自己的想法打的,没有参考如何被人的代码.调试了一天,居然最后错在一个小问题上, ...

  3. zoj1109 水题(大神绕道) Language of FatMouse

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit:32768 KB We all know that FatMouse doe ...

  4. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  5. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  6. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. HDU 5536 Chip Factory 字典树+贪心

    给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...

  8. Phone List 字典树 OR STL

    Phone List Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 140     Solved: 35 Description ...

  9. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

随机推荐

  1. RDD之四:Value型Transformation算子

    处理数据类型为Value型的Transformation算子可以根据RDD变换算子的输入分区与输出分区关系分为以下几种类型: 1)输入分区与输出分区一对一型 2)输入分区与输出分区多对一型 3)输入分 ...

  2. 1082 Read Number in Chinese (25 分)

    1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to ...

  3. ros建立ospf邻居的条件

    Two routers do not become neighbors unless the following conditions are met. Two way communication b ...

  4. https的加密解密是怎么写的?

    原文转载至:http://blog.csdn.net/aqiangsz/article/details/53611665 文章中有些不对的地方,比如用证书对改随机码进行加密,这个是不对,证书本身并没有 ...

  5. crs_stop 错误一列

      http://www.forzw.com/archives/703 grid 与 oracle 版本为11.2.0.4,为两节点RAC,在通过crs_stop -all命令关闭oracle服务时出 ...

  6. 使用amaze ui模板全过程

    amaze ui基于gulp构建,所以现在安装gulp需要的环境,gulp使用npm安装,npm基于node.js 所以一切从node.js开始 1 下载对应的node.js 打开 https://n ...

  7. HtmlRowCreated关于e.Row.Cells[0]的获取和设置

    获取采用:  cmd2.Parameters.AddWithValue("@xh", e.GetValue("学号").ToString().Trim()); ...

  8. 浅谈Matcher和pattern的使用

    这两个类位于java.util.regex包下,主要用于实现正则表达式 Pattern类用于创建一个正则表达式,也可以说是创建一个匹配模式 两个静态方法创建:compile(String regex) ...

  9. 可视化库-seaborn-调色板(第五天)

    1. 基础的调色板的演示  color_palette() 设置传入的任何颜色,不传使用默认颜色,set_palette() 设置所有图的颜色# 6种主题 # 1 deep# 2 muted# 3 p ...

  10. this指针的调整

    我们先来看一段代码: #include <iostream> using namespace std; class A { public: int a; A( ) { printf(&qu ...