zoj1109-Language of FatMouse 【字典树】
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 【字典树】的更多相关文章
- zoj 1109 Language of FatMouse(字典树)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- zoj 1109 zoj 1109 Language of FatMouse(字典树)
好开心,手动自己按照字典树的思想用c写了一个优化后的trie字典树,就是用链表来代替26个大小的字符数组.完全按照自己按照自己的想法打的,没有参考如何被人的代码.调试了一天,居然最后错在一个小问题上, ...
- zoj1109 水题(大神绕道) Language of FatMouse
Language of FatMouse Time Limit: 10 Seconds Memory Limit:32768 KB We all know that FatMouse doe ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- HDU 5536 Chip Factory 字典树+贪心
给你n个数,a1....an,求(ai+aj)^ak最大的值,i不等于j不等于k 思路:先建字典树,暴力i,j每次删除他们,然后贪心找k,再恢复i,j,每次和答案取较大的,就是答案,有关异或的貌似很多 ...
- Phone List 字典树 OR STL
Phone List Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 140 Solved: 35 Description ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
随机推荐
- js阻止默认事件、拖拽等等
1.自定义右键菜单: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> &l ...
- unittest框架进坑系列_(含selenium数据分离的坑)
1.测试用例的执行顺序 有默认的顺序的,不是按你自己的排列执行,注意. 进坑原因,没有先执行制造变量的测试用例,导致其他用例无法找到变量值 2.数据分离的坑 在控制层 有函数嵌套,2个函数都必须带se ...
- 自绘图片下拉项 combobox listbox
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- linux下一个网卡配置多个ip【虚拟ip】
Linux下配置网卡ip别名何谓ip别名?用windows的话说,就是为一个网卡配置多个ip.什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ...
- [UE4]用向量表示方向
向量的概念 一.物理角度的向量 1)向量就是具有大小和长度的量 2)向量就是空间空的箭头 3)向量可以随意平移 举例:力,force:速度,velcity.这些都是具有大小和方向的量,都可以看成是向量 ...
- Delphi Webbrowser使用方法详解(一)
1.webbroser介绍 该组件是一个浏览器组件,可以显示一个指定地址的网页.设置网页打开时的主页以及对网页进行相关的操作,同时也可以对HTML文件进行剪切.复制.粘贴.删除等操作.该 组件在Int ...
- fs和http模块
fs模块写入文件的方式 导入内置模块 const fs=require("fs") 一.异步写入方式 fs.writeFile("写入文件的路径&qu ...
- Survival Coxph log-rank
Difference between survdiff log-rank and coxph log-rank Ask Question 6 1 I'm using the survival pack ...
- 17.scrapy框架简例使用
目标:创建scrapy项目 创建一个spider来抓取站点和处理数据 通过命令行将抓取内容导出 1.创建项目 scrapy startproject tutorial 2.创建spider cd tu ...
- 浏览器。浏览器对象检测、Chrome调试工具
chrome浏览器的flash问题: 2017-12-26 chrome浏览器的flash有无法显示无法正常运行的问题时,解决方法如下: https://qzonestyle.gtimg.cn/qzo ...