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: ...
随机推荐
- 对react vd 性能的理解
相信大家都知道react vd的性能是很好的,速度挺快的,真实dom操作很慢的,但是结果完全相反: 后来我就做了个测试,从两个方面去测试,在页面初始渲染1w条数据,react渲染耗时超过了1秒 在12 ...
- 永恒之蓝EternalBlue复现
0x01 漏洞原理:http://blogs.360.cn/blog/nsa-eternalblue-smb/ 目前已知受影响的 Windows 版本包括但不限于:Windows NT,Windows ...
- 屏蔽各类弹窗广告(WPS、智能云输入法)
托盘中的广告“领取双11红包,最高1111元”的罪魁祸首是“智能云输入法” 广告在托盘中闪动: 结束SCSkinInst.exe后,托盘中的广告消失: 智能云输入法的安装路径可参考: C:\Progr ...
- AttributeError: module 'requests' has no attribute 'get' 遇到了这个错误,是因为我把python关键字做了包名。。。
初学者总会犯各种低级错误,这是其一,特此记录.
- 怎样在 Ubuntu Linux 上安装 MySQL
本教程教你如何在基于 Ubuntu 的 Linux 发行版上安装 MySQL.对于首次使用的用户,你将会学习到如何验证你的安装和第一次怎样去连接 MySQL. -- Sergiu MySQL 是一个典 ...
- 2017.10.29 C/C++/C#程序如何打成DLL动态库
C/C++程序如何打成DLL动态库: **1.在VS中新建main.h,添加如下内容:** extern "C" _declspec(dllexport) int onLoad() ...
- ibator自动代码生成
首先,强烈推荐一篇文章,介绍的特详细 http://www.iteye.com/topic/821983 1. 插件安装 http://blog.csdn.net/rchm8519/article/d ...
- AngularJS THML DOM
AngularJS为HTML Dom元素属性提供了绑定应用数据的指令. data-ng-disabled指令直接提供了绑定应用程序的数据到HTML元素的disabled属性. <!DOCTYPE ...
- jquery 筛选元素 (3)
.addBack() 添加堆栈中元素集合到当前集合中,一个选择性的过滤选择器. .addBack([selector]) selector 一个字符串,其中包括一个选择器表达式,匹配当前元素集合,不包 ...
- Eclipse+Python环境配置
Eclipse+Pydev 1.安装Eclipse Eclipse可以在它的官方网站Eclipse.org找到并下载,通常我们可以选择适合自己的Eclipse版本,比如Eclipse Classic. ...