POJ 2001 Shortest Prefixes(字典树)
Description
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".
Input
Output
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona 题意:求每个字符串最短的与别的字符不相同的前缀
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std; struct trie
{
trie* next[];//下一个结点
int num;//以当前字符串为前缀的数量
trie()//构造函数
{
int i;
for(i=;i<;i++)
next[i]=NULL;
num=;
}
}; trie root; void insert(char word[])
{
trie* r=&root;
int i;
for(i=;word[i];i++)
{
if(r->next[word[i]-'a']==NULL)//这个字符没有
r->next[word[i]-'a']= new trie;
r=r->next[word[i]-'a'];
r->num++;
}
} void find(char word[])
{
trie* r=&root;
int i;
for(i=;word[i];i++)
{
printf("%c",word[i]);
if(r->next[word[i]-'a']==NULL)
return;
r=r->next[word[i]-'a'];
if(r->num==)
return;
}
return ;
} int main()
{
char word[][];
int i=;
while(gets(word[i]))
{
if(word[i][]==NULL)
break;
insert(word[i]);
i++;
}
for(int ii=;ii<i;ii++)
{
printf("%s ",word[ii]);
find(word[ii]);
printf("\n");
}
return ;
}
POJ 2001 Shortest Prefixes(字典树)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
随机推荐
- Maven插件系列之spring-boot-maven-plugin
Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...
- JAVA工程师面试常见问题集锦
集锦一: 一.面试题基础总结 1. JVM结构原理.GC工作机制详解 答:具体参照:JVM结构.GC工作机制详解 ,说到GC,记住两点:1.GC是负责回收所有无任何引用对象的内存空间. 注意: ...
- JDK自带的keytool证书工具详解
一.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore D:/tomcat.keystore -keypass 123456 -store ...
- axure rp安装
axure rp安装 1◆ axure rp 文件下载 2◆创建安装目录 3◆ 安装图解 4◆汉化 替换 5◆ 使用 success 1★AxureRP 8.0安装包 2★ ...
- ubuntu 双硬盘挂载 windows分区自动挂载
sudo fdisk -l 查看硬盘情况 1:新建一个目录,例:old 2:mount /dev/sdb1 old 3:cd old 4:ls (就可以看到新硬盘的内容了) 取消挂载:umoun ...
- linux命令--文件查询
ls [ -lahid ] [ /* ] ls -- 默认查询当前目录下的显性文件 -l -- 显示文件的详细信息 -a -- 显示所有文件(包括隐藏文件) -h -- 文件大小显示为 ...
- 解决Tomcat v6.0 Server at localhost was unable to start within 45 seconds
eclipse 中tomcat启动超时报错如下: Starting Tomcat v7.0 Server at localhost' has encountered a problem Server ...
- bzoj5016
题解: 吧询问变成前缀形式 然后莫队 代码: #include<bits/stdc++.h> ; using namespace std; ]; ,L=,R=; ,Ans[N]; bool ...
- day7-python打开文件方式
文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 基本操作 import codecs #主要用来解决乱码问题 f = codecs.open('1. ...
- [POJ2761]Feed the dogs
Problem 查询区间第k大,但保证区间不互相包含(可以相交) Solution 只需要对每个区间左端点进行排序,那它们的右端点必定单调递增,不然会出现区间包含的情况. 所以我们暴力对下一个区间加上 ...