思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好。

这样查询到某个单词时输出叶子结点存的就行,查不到就"en"呗。这题用hash也是可以的

 #include<iostream>
#include<cstdio>
#include<stdio.h>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std; struct node
{
int cnt;
char c[];//结点所对应的字符
struct node *next[];
node ()
{
cnt = ;
memset(next,,sizeof(next));
}
};
node * root = NULL;//根结点初始为NULL void BuildTrie(char *s,char *temp)
{
node *p = root;
node *tmp = NULL;
int l = strlen(s);
for(int i = ;i < l ;i ++)
{
if(p->next[s[i]-'a'] == NULL)
{
tmp = new node;
p->next[s[i]-'a'] = tmp; }
p = p->next[s[i]-'a'];
}
p->cnt = ;
strcpy(p->c,temp);//存放翻译结果 } void Query(char *s)
{
node *p = root;
int l = strlen(s);
for(int i = ;i< l ;i++)
{
if(p->next[s[i]-'a'] == NULL)
{
printf("eh\n");
return ;
}
p = p->next[s[i]-'a'];
}
printf("%s\n",p->c);
return ;
} void Del(node * root)
{
for(int i = ;i < ;i++)
{
if(root->next[i])
{
Del(root->next[i]);
}
}
} int main()
{
char str[],s1[],s2[];
root = new node;
while(gets(str))
{
if(str[] == '\0')
break;
sscanf(str,"%s %s",s1,s2);
BuildTrie(s2,s1);//注意参数位置
}
while(scanf("%s",str)!=EOF)
Query(str);
return ;
}

POJ2503(Babelfish)--简单字典树的更多相关文章

  1. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  2. poj 2503 Babelfish(字典树哈希)

    Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...

  3. hdu 1251简单字典树

    #include<stdio.h> #include<iostream> #include<string.h> using namespace std; struc ...

  4. HDU 1247 Hat’s Words(字典树)题解

    题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...

  5. UVA 11732 链表+字典树

    因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储.这个倒是不难,练了下手 统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节 #include ...

  6. J - What Are You Talking About(map,字典树)

    题意:上部分是单词表,下部分是句子,翻译句子.START开始,END结束. 思路:简单字典树. Ignatius is so lucky that he met a Martian yesterday ...

  7. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  8. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  9. LA、Remember the Word (字典树, 简单dp)

    传送门 题意: 给你一个初始串 S,strlen(s) <= 3e5  然后给你 n 个单词. n <= 4000,  每个单词的长度不超过 100 : 问你这个初始串,分割成若干个单词的 ...

随机推荐

  1. Android Studio 3.5新特性

    Android Studio 3.5新特性     原文链接:https://blog.csdn.net/jklwan/article/details/99974869 Android Studio ...

  2. angular中的服务和持久化实现

    1.创建服务: ng g service my-new-service 创建到指定目录下面 ng g service services/storage 2.app.module.ts 里面引入创建的服 ...

  3. Centos7彻底删除PHP

    查看php版本命令: #php -v 下面的命令是删除不干净的 #yum remove php 因为使用这个命令以后再用 #php -v 还是会看到有版本信息的..... 必须强制删除,使用下面命令查 ...

  4. F2812 DSP程序运行在片内RAM和FLASH的区别

    F2812 DSP程序运行在片内RAM和片内FLASH的区别 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:F2812是带有内部Flash的DSP,与 ...

  5. 最新 字节跳动java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.字节跳动等10家互联网公司的校招Offer,因为某些自身原因最终选择了字节跳动.6.7月主要是做系统复习.项目复盘.Leet ...

  6. selenium 获取不了标签文本的解决方法

    selenium 获取不了标签文本的解决方法 ------ 即driver.find_element_by_xxx().text() 为空的解决办法 如果得到的文本只为空,而非我们期望的baidu,那 ...

  7. 乐字节Java编程之方法、调用、重载、递归

    一.概述 方法是指人们在实践过程中为达到一定目的和效果所采取的办法.手段和解决方案. 所谓方法,就是解决一类问题的代码的有序组合,是一个功能模块.编程语言中的方法是组合在一起来执行操作语句的集合.例如 ...

  8. 工作总结 CTO(张王岩) File构造器

    import java.io.File; /** * 构建File对象 * @author Allen17805272076 * */ public class FileDemo2 { public ...

  9. Word 插入目录的 5 种方法

    1. "运用多级编号法"之图文教程 效果图: 步骤: 首先,为了更好的演示,我们先将我们的一级标题全部选中,按住Ctrl键,去一一选择就行了. 选中一级标题后,我们进入「开始」-「 ...

  10. [NOIP提高组2018]货币系统

    [TOC] 题目名称:货币系统 来源:2018年NOIP提高组 链接 博客链接 CSDN 洛谷博客 洛谷题解 题目链接 LibreOJ(2951) 洛谷(P5020) 大视野在线评测(1425) 题目 ...