poj2001Shortest Prefixes(trie)
Shortest Prefixes
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 18687 | Accepted: 8087 |
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 题目大意:给出一堆字符串,给每一个字符串找最小的前缀,让所有的前缀都不重复。
字典树询问时,找到一个val为1的说明这个点只有他自己一个,所以到此结束。
注意数组的大小
#include<cstdio>
#include<cstring>
#define MAXN 100100 char s[][]; struct Trie
{
int ch[MAXN][];
int val[MAXN];
int size;
Trie()
{
size = ;
memset(ch,,sizeof(ch));
memset(val,,sizeof(val));
}
int id(char c)
{
return c-'a';
}
void Ins(char* s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = id(s[i]);
if (!ch[u][c]) ch[u][c] = size++;
u = ch[u][c];
val[u]++;
}
}
void Find(char* s)
{
int u = , len = strlen(s);
for (int i=; i<len; ++i)
{
int c = id(s[i]);
u = ch[u][c];
printf("%c",s[i]);
if (val[u]==) return ;
}
}
}t; int main()
{
int p = ;
while (scanf("%s",s[++p])!=EOF)
{
t.Ins(s[p]);
}
for (int i=; i<=p; ++i)
{
printf("%s ",s[i]);
t.Find(s[i]);
printf("\n");
}
return ;
}
poj2001Shortest Prefixes(trie)的更多相关文章
- POJ2001Shortest Prefixes(Trie树)
传送门 题目大意:求最短唯一前缀 题解:Trie树 把单词一个个插入,每个字母节点v[]++;然后输出时输出到v[]为1的点, v[]=1说明只有这个单词经过. 代码 : #include<io ...
- poj2001 Shortest Prefixes (trie)
读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- LA3942-Remember the Word(Trie)
题意: 有s个不同的单词,给出一个长字符串把这个字符串分解成若干个单词的连接(可重复使用),有多少种分解方法 分析: dp[i]表示i开始的字符串能分解的方法数 dp[i]=sum(dp[i+len( ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- codeforces 380A Sereja and Prefixes (递归)
题目: A. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input st ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- Shortest Prefixes(trie树唯一标识)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15948 Accepted: 688 ...
- POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...
随机推荐
- lunix重启service network restart错误Job for network.service failed. See 'system 或Failed to start LSB: Bring
1.mac地址不对 通过ip addr查看mac地址,然后修改cd /etc/sysconfig/network-scripts/目录下的文件里面的mac地址 2.通过以下方法 systemctl s ...
- 概念:详细讲解url和路由概念
例如:一个网址为 http://www.abc.com/aa 定义:/aa = bb/cc/dd 那么:http://www.abc.com/aa就是一个url,那么我们可以得出:网址=url 而当我 ...
- 科大讯飞语音转文字以及中文分词的Java测试代码
我录了一段音存储在这个test.m4a文件里,语音内容为"测试一下Netweaver对于并发请求的响应性能". 使用如下Java代码进行测试: package com.iflyte ...
- *389. Find the Difference (string + map(26)) read problems carefully
Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...
- gearman安装实录
花了5个小时装好了gearman,问题不断,坑爹的服务器yum还坏了,悲催. 服务器系统:centos5.3 64位 gearman版本:1.1.8 安装包(相关依赖)下载 1.gearman安装包 ...
- 2017.10.9 JVM入门学习
1.什么是JVM JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现 ...
- EF Database first 中,实现 多个表对应一个 实体的 查询
1.首先 创建好 数据 库. hobby表 major 表 student 表 外键 关系如下 2. 实现将 数据库 映射到EDM中 视图如下 在VS中 生成了 3个实体类 ,对应的是 数据库中的3 ...
- LIS的string用法
题目链接 使用的是string里的find函数 stl大法好 #include<iostream> #include<cstdio> #include<cstring&g ...
- webapi中配置返回的时间数据格式
web api返回的是标准格式UTC时间,如果要转成我们需要的格式,可以在WebApiConfig.cs的Register函数中新增以下配置来定义返回的时间类型格式: //配置返回的时间类型数据格式 ...
- C#面向对象的基本概念
“面向对象=对象+类+继承+通信”.如果一个软件系统使用了这样四个概念进行设计和实现,我们就可以认为这个软件系统是面向对象的. 一.一切都是对象 1. 对象概述 对象可以表示几乎所有的实物和概念.比如 ...