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

这样查询到某个单词时输出叶子结点存的就行,查不到就"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. Ionic 的安装运行

    1.学习前准备工作 1.必须得安装 nodejs (建议安装最新的稳定版本) 2.必须有 Angular 基础:https://www.loaderman.com/goods-1047.html 2. ...

  2. Faster RCNN论文学习

    Faster R-CNN在Fast R-CNN的基础上的改进就是不再使用选择性搜索方法来提取框,效率慢,而是使用RPN网络来取代选择性搜索方法,不仅提高了速度,精确度也更高了 Faster R-CNN ...

  3. float和int转换

    http://blog.sina.com.cn/s/blog_5c6f79380101bbrd.html https://blog.csdn.net/ganxingming/article/detai ...

  4. 使用select和show命令查看mysql数据库系统信息

    (1).select 显示当前日期和时间 mysql> select now(); +---------------------+ | now() | +-------------------- ...

  5. python使用退格键时出现^H解决方法

    Linux 使用退格键时出现^H解决方法 1.临时解决 按ctrl 2.永久解决 基本现象 进入 Python shell,按下 Delete/Backspace 键: Python 3.5.2 (d ...

  6. 【Leetcode_easy】754. Reach a Number

    problem 754. Reach a Number solution1: class Solution { public: int reachNumber(int target) { target ...

  7. (转)华为 安卓手机在MAC系统下 ADB 识别

    使用MACOS发现在Android开发环境完整的情况下,接入小米,SAMSUNG,HTC,ZTE等手机都可以自动识别,如果暂时不能识别,只需要在 adb_usb.ini 中设置之后也可以识别,并可以在 ...

  8. 【c# 学习笔记】面向对象编程的应用

    在平时的开发过程中,面向对象编程的应用肯定必不可少.但前面的内容只是单独介绍了类.面向对象思想和接口,那么我们怎么在平时工作中来应用他们来实现面向对象编程呢? 如果你想设计一个Dog类,有了类的概念后 ...

  9. 【c# 学习笔记】类与结构体的区别

    由于类与结构体在语法和使用上都非常类似,导致我们这些初学者容易混淆.为更好理解,特做以下区分记录: ① 语法上的区别在于,定义类要使用关键词class,而定义结构体则使用关键词struct. ② 结构 ...

  10. Flutter 弹出键盘屏幕溢出问题

    在使用输入框获取焦点,弹出键盘的时候,会导致屏幕溢出, 解决办法: resizeToAvoidBottomPadding: false, //输入框抵住键盘 return Scaffold( appB ...