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 ...
随机推荐
- kohana task 编写计划任务
kohana 框架 我们经常使用gleez作为我们二次开发. 收先我们要把文件建在Task文件夹下,比如新建文件为:testcron <?phpdefined('SYSPATH') or di ...
- Oracle 11.2.0.4.0 Dataguard部署和日常维护(6)-Dataguard Snapshot篇
1. 检查当前主备库同步状态 on primary select ads.dest_id,max(sequence#) "Current Sequence", max(log_se ...
- Maximum sub array
Here I post a way to solve maximum sub array problem: The problem described like this: here is an ar ...
- npm常用功能
1. npm -v在命令行中输入该代码,可以查看npm当前版本号 2.安装依赖包2.1 npm install <name>先使用cd命令跳转到需要安装模块的目录,在该目录下执行np ...
- [LeetCode] 100. Same Tree ☆(两个二叉树是否相同)
描述 解析 根与根比较,左右子树互相递归比较即可. 代码 /** * Definition for a binary tree node. * public class TreeNode { * in ...
- android 数据库添加字符串 添加失败 解决方案
这两天遇到一个棘手的问题,在往sqlite数据库中添加数据时,总是添加失败,但是添加数字却可以.原来是添加时,忘记添加''号修饰. 修改前: 修改后: 这样就完美解决.
- java⑩
1.for循环: for循环语法 for(表达式1;表达式2;表达式3){ 循环体4} 表达式1:初始化变量 只执行一次!表达式2:循环条件 满足条件进入循环体4表达式3:迭代变量 如果循环体 中只有 ...
- Docker安装websphere(四)
在Docker容器里安装webshpere <!--前提:已经安装好了docker,能够正常使用.--> (1)docker安装websphere(需要账号和密码登录,不挂载数据卷) 获取 ...
- 据说excel流是这么做,上次我分享的是csv格式。这个是excel格式。
import xlwt import StringIO import web urls = ( '/rim_request','rim_request', '/rim_export','rim_exp ...
- java IO实例
import java.io.*; /** * Created by CLY on 2017/7/23. */ public class Main { public static void main( ...