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. python学习笔记--pycurl模块安装遇到的问题。

    1.用easy_install安装的时候 [root@idayuan ~]# easy_install pycurl Searching for pycurl Best match: pycurl A ...

  2. DDA画线算法

    #include<stdio.h> #include"graphics.h" #include<math.h> #include<stdlib.h&g ...

  3. [UE4]为UStaticMeshComponent添加点击事件

    BlockMesh->OnClicked.AddDynamic(this, &APuzzleBlock::BlockClicked); //鼠标点击事件 BlockMesh->On ...

  4. API网关Kong系列(四)认证配置

    目前根据业务需要先介绍2种认证插件:Key Authentication 及 HMAC-SHA1 认证  Key Authentication 向API添加密钥身份验证(也称为API密钥). 然后,消 ...

  5. java中的URLConnection和HttpURLConnection有什么区别(因为我自己搜到别人写的区别看下来都没有什么区别)

    今天看了一下公司同事的代码,如下 URLConnection connection = openConnection(localURL); HttpURLConnection httpURLConne ...

  6. ORACLE常用数值函数、转换函数、字符串函数介绍

    ORACLE常用数值函数.转换函数.字符串函数介绍. 数值函数: abs(m) m的绝对值 mod(m,n) m被n除后的余数 power(m,n) m的n次方 round(m[,n]) m四舍五入至 ...

  7. collections模块---(namedtuple、deque、OrderdDict、defaultdict、Counter)和configparser模块

    在内置数据类型(dict. list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter. deque.defaultdict.namedtuple 和 ...

  8. laravel5.4中ajax删除数据

    1 JS代码 function deleteInfo(id) { if(id) { var r=confirm('确定要删除吗'); if(r==true) { $.ajax({ url: " ...

  9. (11/24) css进阶:Less文件的打包和分离

    写在前面:在前面我们对css打包和分离进行了描述.此节我们开始学习如何对less文件进行打包和分离. Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性, ...

  10. 2.mybatis实战教程(mybatis in action)之二:以接口的方式编程

    转自:http://www.yihaomen.com/article/java/304.htm 前面一章,已经搭建好了eclipse,mybatis,mysql的环境,并且实现了一个简单的查询. 请注 ...