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 ...
随机推荐
- 2016年10月10日--穷举、迭代、while循环
穷举 将所有可能性全部全部走一遍,使用IF筛选出满足的情况 练习: 1.单位给发了一张150元购物卡, 拿着到超市买三类洗化用品. 洗发水15元,香皂2元,牙刷5元. 求刚好花完150元,有多少种买法 ...
- HDU 1533 最小费用最大流(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1533 这道题直接用了模板 题意:要构建一个二分图,家对应人,连线的权值就是最短距离,求最小费用 要注意void ...
- 转: Annovar 软件注释流程介绍
第一步:下载Annovar 上Annovar官网下载(http://annovar.openbioinformatics.org/en/latest/user-guide/download/),现在要 ...
- replace、replaceAll、replaceFirst的区别详解
String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...
- 2.5---链表来进行加法,链式A+B(CC150)
这里是用了自己的方法,和书本不一样. import java.util.HashSet; import java.util.Set; class ListNode{ int val; ListNode ...
- Service错误
错误日志: Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act= ...
- C#错误之 System.Threading.ThreadAbortException:正在中止线程
参考:http://www.cnblogs.com/chendaoyin/archive/2013/06/27/3159211.html 1.开启一个子线程 //开启一个子线程,子线程调用方法 Met ...
- poj 1028
http://poj.org/problem?id=1028 题意(水):做一个游览器的历史记录. back:后退到上一个页面,当上一个页面没有时,输出ignored. forward:向前一个页面, ...
- iOS gcd dispatch使用注意,dispatch_syn可能产生的死锁
我们在使用dispatch_sync 时可能会出现死锁,看下面的例子: import UIKit class ViewController: UIViewController { var seri ...
- C# 读写十六进制bin 文件
读一个十六进制的bin文件,在bin文件添加四行头,生成新的bin文件.bin文件可以用vs打开查看. using System; using System.Collections.Generic; ...