字典树(POJ 2503)
它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。 它有3个基本性质:
根节点不包含字符,除根节点外每一个节点都只包含一个字符; 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串; 每个节点的所有子节点包含的字符都不相同。 #include <iostream>
#include<cstring>
#include<cstdio> using namespace std; const int M=;
char word[][];
int wp;
struct node
{
int next[];
int value;
bool end;
}tree[M]; int pi=; void init() //初始化
{
memset(tree,,sizeof(tree)); //将一个数组全部元素赋成0 //只能是-1和0
pi=;
wp=;
} void insert(char * keyword,int value) //建树
{
int index,p,i;
for(i=p=;keyword[i];i++)
{
index=keyword[i]-'a'; //
if(tree[p].next[index]==) //
tree[p].next[index]=pi++; p=tree[p].next[index];
}
tree[p].value=value;
tree[p].end=;
} bool query(char * keyword,int& value)
{
int index,p,i;
for(i=p=;keyword[i];i++)
{
index=keyword[i]-'a';
if(tree[p].next[index]==)
return ;
p=tree[p].next[index];
}
if(tree[p].end)
{
value=tree[p].value;
return ;
}
return ;
} int main()
{
char s1[],s2[],s[];
int i,value;
while(gets(s))
{
if(!strlen(s)) //有字母就不跳出
break; for(i=;i<strlen(s);i++)
{
if(s[i]==' ') //空格
break;
}
strncpy(s1,s,i); //把s的前i位复制给s1
s1[i]=;
strcpy(s2,s+i+); //把s的i位之后复制给s2
strcpy(word[wp],s1); //
insert(s2,wp++); //
}
while(scanf("%s",s)!=EOF)
{
if(query(s,value)) //到相应字母 然后输出
printf("%s\n",word[value]);
else
printf("eh\n"); //没找到输出eh
}
return ;
}
字典树(POJ 2503)的更多相关文章
- [字典树] poj 2418 Hardwood Species
题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS Memory ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- 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 ...
- poj 2503 哈希 Map 字典树
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36967 Accepted: 15749 Descr ...
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...
- POJ 2418 字典树
题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...
随机推荐
- sdut oj 1163 C语言实验——排列 (当初不会递归生成排列,这个题目现在才补上 刘汝佳给出了写法 *【模板】 当然有生成全排列的函数存在 )
C语言实验——排列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 有4个互不相同的数字,请按序输出由其中三个不重复数字组成的排列 ...
- PYTHON 爬虫笔记二:Urllib库基本使用
知识点一:urllib的详解及基本使用方法 一.基本介绍 urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的 ...
- PHP执行外部命令【转】
PHP是完全支持外部命令的,但是出于安全考虑,一般很少使用. PHP提供共了3种方法调用外部命令: (1)调用执行外部命令函数(system(),exec(),passthru(),shell_exe ...
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(Bash Game)
题意:从0开始捐款,每次不超过m元,首先达到n元的获胜 思路:等同于从n开始,每次取不超过m,首先达到0的获胜.(Bash Game) #include<iostream> #includ ...
- ubuntu下安装cpython 0.2x
Quick installation of cython: Step 1: Update system: sudo apt-get update Step 2: Install: cython Ate ...
- HihoCoder 1638 : 小Hi的天平 (2-sat+并查集)
描述 小Hi给小Ho邮寄了一个天平.收到天平后,小Ho想知道天平在运输过程中是否损坏,为此它准备了A类物品和B类物品共n个(可能只有A类物品,也可能只有B类物品),但无法确定一个物品是哪一类.A类物品 ...
- 解决Exception:Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext
原因是配置文件中 <bean id="transactionManager" class="org.springframework.orm.hibernate4.H ...
- Eigen::aligned_allocator
http://blog.csdn.net/rs_huangzs/article/details/50574141
- 【网络爬虫】【python】网络爬虫(二):网易微博爬虫软件开发实例(附软件源码)
对于urllib2的学习,这里先推荐一个教程<IronPython In Action>,上面有很多简明例子,并且也有很详尽的原理解释:http://www.voidspace.org.u ...
- 微信小程序开发之三元运算符代替wx.if/wx.else
直接上代码 实现功能为:当fbphotoFirst为空时,src路径为“pic/信息反馈1-1_14.png“,并且点击事件uploadfbphotoFirst有效,否则为路径fbphotoFirst ...