它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。

它有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)的更多相关文章

  1. [字典树] poj 2418 Hardwood Species

    题目链接: id=2418">http://poj.org/problem?id=2418 Hardwood Species Time Limit: 10000MS   Memory ...

  2. POJ 2503 字典树

    题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2]  意思是string2的值为string1. 然后给定一波 ...

  3. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

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

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

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

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

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

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

  7. poj 2503 哈希 Map 字典树

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 36967   Accepted: 15749 Descr ...

  8. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  9. POJ 2418 字典树

    题目链接:http://poj.org/problem?id=2418 题意:给定一堆树的名字,现在问你每一棵树[无重复]的出现的百分比,并按树名的字典序输出 思路:最简单的就是用map来写,关于字典 ...

随机推荐

  1. 远程请求json数据,list中显示

    public class MainActivity extends Activity { protected static final int WHAT_REQUEST_SUCCESS = 1; pr ...

  2. iOS 第三方登录之 QQ登录

    一. 首先需要下载腾讯qq登录所需的库,下载地址是http://open.qq.com/ . 需要用到的有TencentOpenAPI.framework 和TencentOpenApi_IOS_Bu ...

  3. Hadoop开发

    HDFS HDFS提供一套Java API来操作HDFS,包括文件的建立.修改.删除.权限管理等,下面对几个常用的API进行介绍,详细的API接口请参见API文档,可以在${HADOOP_HOME}/ ...

  4. return;测试

    一 没有return;,则会顺序执行到最后 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

  5. 为什要使用预编译SQL?

    今天在研发部技术大牛的指点下,我终于明白了为什么要使用SQL预编译的形式执行数据库JDBC:

  6. hdu 2899 Strange fuction —— 模拟退火

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899 模拟退火: 怎么也过不了,竟然是忘了写 lst = tmp ... 还是挺容易A的. 代码如下: # ...

  7. Azure ARM (22) Azure Policy入门

    <Windows Azure Platform 系列文章目录> 我们知道,在Azure服务层级中,分为以下几个层次: 1.企业合同 2.订阅 3.资源组 4.资源 我们使用的Azure资源 ...

  8. TortoiseGit创建本地库并提交到远程服务器

    前半部分参考网上的例子:http://www.showerlee.com/archives/1300,但会出现“Git did not exit cleanly (exit code 128)”错误 ...

  9. zookeeper分布式安装

    1.先把zookeeper-3.5.2-alpha.tar上传解压到/usr/local/下重命名为zookeeper ############# zookeeper env ############ ...

  10. ERROR (UnicodeEncodeError): 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128

    ERROR (UnicodeEncodeError): 'ascii' codec can't encode characters in position 0-1: ordinal not in ra ...