POJ2503(Babelfish)--简单字典树
思路:就是用一个字典树翻译单词的问题,我们用题目中给出的看不懂的那些单词建树,这样到每个单词的叶子结点中存放原来对应的单词就好。
这样查询到某个单词时输出叶子结点存的就行,查不到就"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)--简单字典树的更多相关文章
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2503 Babelfish(字典树哈希)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...
- hdu 1251简单字典树
#include<stdio.h> #include<iostream> #include<string.h> using namespace std; struc ...
- HDU 1247 Hat’s Words(字典树)题解
题意:给一个字符串集,要你给出n个字符串s,使s能被所给字符串集中的两个相加所得(ahat=a+hat) 思路:简单字典树题,注意查询的时候要判断所指next是否为NULL,否则会RE非法访问. 代价 ...
- UVA 11732 链表+字典树
因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储.这个倒是不难,练了下手 统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节 #include ...
- J - What Are You Talking About(map,字典树)
题意:上部分是单词表,下部分是句子,翻译句子.START开始,END结束. 思路:简单字典树. Ignatius is so lucky that he met a Martian yesterday ...
- poj 2503 Babelfish(字典树或着STL)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35828 Accepted: 15320 Descr ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- LA、Remember the Word (字典树, 简单dp)
传送门 题意: 给你一个初始串 S,strlen(s) <= 3e5 然后给你 n 个单词. n <= 4000, 每个单词的长度不超过 100 : 问你这个初始串,分割成若干个单词的 ...
随机推荐
- 如何git revert merge commit?
答: git revert -m <parent-number> <commit-id> (适用于merge操作的commit) 参考资料: https://blog.csdn ...
- Unix/Linux系统下获得时间戳函数
在Unix/Linux系统下,使用gettimeofday函数来获得当前系统的时间戳,精度可达到微秒(microsecond,即μs)级别. 通过结构体timeval来存放当前时间戳的信息: #ifn ...
- 简单模拟CO模块
promise方式: // 对co模块的简单模拟 function run(gen){ var g = gen(); function next(data){ var result = g.next( ...
- springboot集成调用Azkaban
springboot集成调用Azkaban 一. 说明 1.Azkaban是由Linkedin公司推出的一个批量工作流任务调度器,主要用于在一个工作流内以一个特定的顺序运行一组工作和流程,它的配置是通 ...
- 解析python 命令的-u参数
在shell脚本中运行python 命令时后面加了-u 参数(python -u xx.py),这个-u表示什么? import sys sys.stdout.write("stdout1& ...
- kafka shell file
1. start kafka and schema_registry #!/bin/sh export KAFKA_HOME=/home/lenmom/workspace/software/confl ...
- Jsoup-简单爬取知乎推荐页面(附:get_agent())
总览 今天我们就来小用一下Jsoup,从一个整体的角度来看一看爬虫 一个基本的爬虫框架包括: [x] 解析网页 [x] 失败重试 [x] 抓取内容保存至本地 [x] 多线程抓取 *** 分模块讲解 将 ...
- JBPM使用
jbpm+mysql5.7 https://blog.csdn.net/tyn243222791/article/details/79033555
- MapReduce\Tez\Storm\Spark四个框架的异同
1) MapReduce:是一种离线计算框架,将一个算法抽象成Map和Reduce两个阶段进行 处理,非常适合数据密集型计算. 2) Spark:MapReduce计算框架不适合迭代计算和交互式计算, ...
- python面向对象之类属性,实例属性
python中的属性分为类属性和实例属性,之前已经说过一些,这里主要是对类属性与实例属性的增删改查 首先是对类属性的增删改查,下面这个是对类属性的修改,在书写类时,已经对类属性occupation进行 ...