poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 30816 | Accepted: 13283 |
Description
Input
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Source
#include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
struct Tire{
Tire *next[];
char *trans;
Tire()
{
int i;
for(i=;i<;i++)
next[i] = NULL;
trans = NULL;
}
} root; void Insert(char word[],char trans[])
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i] - 'a';
if(p->next[t]==NULL)
p->next[t] = new Tire;
p = p->next[t];
}
p->trans = new char[];
strcpy(p->trans,trans);
} void Find(char word[]) //查找该单词的翻译并输出,如果没有则输出eh
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i] - 'a';
if(p->next[t]==NULL){
printf("eh\n");
return ;
}
p = p->next[t];
}
if(p->trans!=NULL)
printf("%s\n",p->trans);
else //没找到
printf("eh");
} int main()
{
char str[];
while(gets(str)){
if(str[]=='\0') //检测到空行
break;
char trans[],word[];
sscanf(str,"%s%s",trans,word);
Insert(word,trans);
}
while(scanf("%s",str)!=EOF) //读取要翻译的单词
Find(str);
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 2503:Babelfish(字典树,经典题,字典翻译)的更多相关文章
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- HDU 1251 统计难题(字典树模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...
- hdu1305 字典树水题
题意: 给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路: 字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...
- 字典树&&01字典树专题&&对字典树的理解
对于字典树和01字典树的一点理解: 首先,字典树建树的过程就是按照每个数的前缀来的,如果你要存储一个全小写字母字符串,那么这个树每一个节点最多26个节点,这样的话,如果要找特定的单词的话,按照建树的方 ...
- poj 2503 Babelfish(字典树或着STL)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35828 Accepted: 15320 Descr ...
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
随机推荐
- net-snmp的dateandtime数据类型
net-snmp的dateandtime数据类型 2015/06/12 16:35:59 DateAndTime是Snmpv2中的一种数据类型,它主要提供了对日期时间的描述. 在开发一个snmp相关程 ...
- Python自动化之异常处理
1 异常基本结构 try: pass except Exception,ex: pass 2 异常种类 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x ...
- Android Xpose Hook(一)
实验环境: Droid4x模拟器 (目前Android版本4.2.2) Android Studio 1.下载相关工具 XposedInstaller下载 http://repo.xp ...
- chkconfig用法 LINUX
chkconfig用法 有时候为了方便管理,我们常常喜欢在Linux中将之安装为服务,然后就可以使用服务来管理. 但是当我们运行安装服务的命令时候,假设服务名为myservice #chkconfig ...
- kill新号专题
一.在tomcat启动脚本中看到,kill -3
- Binary Tree Upside Down
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
- Qt ini配置文件读写
#include <QSettings> void MainWindow::saveIni() { //如果ini文件不存在,创建新ini文件,存在赋值 QSettings *setIni ...
- delphi控件属性大全-详解-简介
http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...
- 和我一起学python,初识python (life is short ,we need python)
作者:tobecrazy 出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 由于项目需要(并非因为life is short), ...
- 从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中
1,实现功能:从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中 并实现app向后移动一个元素的位置: 2.实现思路: 01.遍历下拉菜单,添加拖拽方法,实现位置移动功能: 02.遍历app列 ...