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. Mybatis学习(2)原始dao开发和使用mapper接口代理开发

    基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...

  2. [C#][MVC]DropDownListFor 默认值无法选中的 BUG

    本文来自:https://www.cnblogs.com/craze/p/6124575.html 关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无 ...

  3. 2018ICPC网络赛(徐州站)A题题解

    一.题目链接 https://nanti.jisuanke.com/t/31453 二.题意 给定$N$个位置,$2^k$种颜色,让你去涂色,条件是相邻的两种颜色类型异或值的二进制表示不全为$1$(以 ...

  4. ie下 iframe在页面中显示白色背景 如何去掉的问题

    ie下:

  5. 关于sencha touch在华为、红米等部分手机下hide事件失效,msgbox无法关闭的解决方案(已更新最新解决方案)

    (急着解决问题的同学可以直接跳最底部查看最终的解决方案) 问题描述 因为前段时间抢到了华为荣耀3c,所以做项目的时候就用荣耀3c测试了一下项目, 结果发现在华为的emotion ui上sencha t ...

  6. django-媒体文件,图片存储

    1.settings.py # 媒体文件 MEDIA_ROOT = 'media/'

  7. python you-get 下载视频

    python使用you-get模块下载视频   pip install you-get # 安装先 怎么用    进入命令行: you-get url 暂停下载:ctrl + c ,继续下载重复  y ...

  8. 今天练手了下mysqlbinlog,标记下

    1 首先查看是否开启了 bin log 登录mysql后  使用命令 show variables like "log_%"; show binary logs; 2  确认开启了 ...

  9. C# WebBrowser

    WebBrowser中的按钮调用WinForm中的事件: private void Form1_Load(object sender, EventArgs e) { this.webBrowser1. ...

  10. spring boot 自定义异常

    1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...