zoj 1109 Language of FatMouse(字典树)
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 beprepared since our nation will join WTO soon. Thanks to Turing we havecomputers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blankline, followed by a message of up to 100,005 words. Each dictionaryentry is a line containing an English word, followed by a space and a FatMouse word.No FatMouse word appears more than
once in thedictionary. 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 10lowercase 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 题解:建科字典树,单词插入的结尾记录该单词相应的单词的下标,查询就可以。#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstdlib> #define N 100020 using namespace std; char s[12];
char ans[N][12]; struct Trie {
int id;
struct Trie *nxt[26];
Trie() {
id=0;
for(int i=0; i<26; i++) {
nxt[i]=NULL;
}
}
}; void Trie_Inser(Trie *p,char s[],int id) {
int i=0;
Trie *q=p;
while(s[i]) {
int nx=s[i]-'a';
if(q->nxt[nx]==NULL) {
q->nxt[nx]=new Trie;
}
i++;
q=q->nxt[nx];
}
q->id=id;
} int Trie_Serch(Trie *p,char s[]) {
Trie *q=p;
int i=0;
while(s[i]) {
int nx=s[i]-'a';
if(q->nxt[nx]==NULL)return 0;
q=q->nxt[nx];
i++;
}
return q->id;
} int main() {
//freopen("test.in","r",stdin);
Trie *p=new Trie;
int id=1;
char a[40];
while(1) {
gets(a);
if(a[0]=='\0')break;
int l=0;
int len=strlen(a);
int i;
for(i=0; i<len; i++) {
if(a[i]==' ')break;
ans[id][l++]=a[i];
}
ans[id][l]='\0';
l=0;
i++;
for(; i<len; i++) {
s[l++]=a[i];
}
s[l]='\0';
//printf("%s %s\n",ans[id],s);
Trie_Inser(p,s,id);
id++;
}
while(~scanf("%s",s)) {
id=Trie_Serch(p,s);
if(id==0) {
printf("eh\n");
} else {
printf("%s\n",ans[id]);
}
}
return 0;
}
zoj 1109 Language of FatMouse(字典树)的更多相关文章
- zoj 1109 zoj 1109 Language of FatMouse(字典树)
好开心,手动自己按照字典树的思想用c写了一个优化后的trie字典树,就是用链表来代替26个大小的字符数组.完全按照自己按照自己的想法打的,没有参考如何被人的代码.调试了一天,居然最后错在一个小问题上, ...
- zoj 1109 Language of FatMouse(map映照容器的典型应用)
题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...
- ZOJ 1109 Language of FatMouse
较简单字典树,每输入一对字符串,前一个放在字典(数组)中,后一个插入字典树中,并将其最终的flag赋为前一个在数组中的下标,再就好找了.输入的处理方法要注意一下. 代码: #include <i ...
- zoj 1109 Language of FatMouse(map)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- ZOJ 1109 Language of FatMouse 【Trie树】
<题目链接> 题目大意: 刚开始每行输入两个单词,第二个单词存入单词库,并且每行第二个单词映射着对应的第一个单词.然后每行输入一个单词,如果单词库中有相同的单词,则输出它对应的那个单词,否 ...
- zoj 1109 Language of FatMouse 解题报告
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 题目意思:给出一个mouse-english词典,问对于输入的m ...
- ZOJ Problem Set - 1109 Language of FatMouse
这道题目最让人头疼的就是该题的input怎么结束,因为它要求输入一个空行的时候则一串字符串输入结束,这就不得不让人绕个弯来解决这个问题. (注:本人习惯于使用C中的字符串操作,但是用到map要求使用s ...
- zoj1109-Language of FatMouse 【字典树】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...
- ZOJ 3674 Search in the Wiki(字典树 + map + vector)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每一个单词都一些tips单词. 先输入n个单词和他们的t ...
随机推荐
- oracle 10g函数大全--字符型函数
ASCII(x1) [功能]:返回字符表达式最左端字符的ASCII 码值. [参数]:x1,字符表达式 [返回]:数值型 [示例] SQL> select ascii('A') A,ascii( ...
- http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html
http://www.cnblogs.com/ITtangtang/archive/2012/05/21/2511749.html http://blog.sina.com.cn/s/blog_538 ...
- css border-sizing 用法与理解
浏览器支持 IE Firefox Chrome Safari Opera 支持 支持 支持 支持 支持 Internet Explorer.Opera 以及 Chrome 支持 box-si ...
- ArrayList源码深度解析
jdk:1.8 一.先看看ArrayList类的整体概述, ArraList是基于动态数组实现的一种线性列表,这种基于动态数组的好处就是索引比较快,时间复杂度为O(1):但是对数据修改比较慢,因为需要 ...
- Caused by: org.apache.ibatis.reflection.ReflectionException我碰到的情况,原因不唯一
映射文件: <select id="selectKeyByUserId" resultMap="Xxx"> <![CDATA[ ...
- Joiner的用法
Google Guava提供了Joiner类专门用来连接String. 譬如说有个String数组,里面有"a","b","c",我们可以通 ...
- java二维码小试牛刀
旁白: 由于工作需要,要做一个java的二维码的图片,花了2天的时间学习了一下,过程中也遇到了一些问题,这里做个笔记,收藏了. 废话不多说了,入题吧! 转自:http://www.open-open. ...
- UTC时间格式转换
如‘2018-08-07T14:44:40.000+0800’时间转换为正常时间格式 使用moment库 import moment from 'moment' // 日期格式化 formatTime ...
- OJ刷题---简单password破解
题目要求: 输入代码: #include<iostream> #include <cstdio> #include <cstring> using namespac ...
- VBA验证工作表是否存在
使用VBA验证工作表是否存在 ============================================================= 代码区域 ================== ...