poj2001 Shortest Prefixes (trie)
读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数。
然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了。
不知为何下面的程序一直有bug……不知是读入的问题?
type node=record
w:longint;
go:array['a'..'z'] of longint;
end;
var i,n,tot:longint;
s:string;
a,ans:array[..] of string;
t:array[..] of node;
procedure getintree(s:string);
var i,now:longint;
begin
now:=;
for i:= to length(s) do
begin
inc(t[now].w);
if t[now].go[s[i]]<> then now:=t[now].go[s[i]]
else
begin
inc(tot);
fillchar(t[tot].go,sizeof(t[tot].go),);
t[now].go[s[i]]:=tot;
now:=tot;
end;
end;
end;
procedure check(s:string;x:longint);
var i,now:longint;
flag:boolean;
st:string;
begin
now:=;i:=;
flag:=false;
st:='';
repeat
inc(i);st:=st+s[i];
now:=t[now].go[s[i]];
if t[now].w= then flag:=true;
until (flag) or (i=length(s));
ans[x]:=st;
end;
begin
while true do
begin
readln(s);if s='' then break;
inc(n);a[n]:=s;
getintree(s);
end;
for i:= to n do check(a[i],i);
for i:= to n do writeln(a[i],' ',ans[i]);
end.
poj2001 Shortest Prefixes (trie)的更多相关文章
- POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...
- Shortest Prefixes(trie树唯一标识)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15948 Accepted: 688 ...
- poj2001Shortest Prefixes(trie)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18687 Accepted: 808 ...
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
- Leetcode 943. Find the Shortest Superstring(DP)
题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- 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 ...
随机推荐
- Delphi摄像头操作
/*Title:Delphi摄像头操作 *Author:Insun *Blog:http://yxmhero1989.blog.163.com *From:www.4safer.com */ 为了笔耕 ...
- Spark小课堂Week2 Hello Streaming
Spark小课堂Week2 Hello Streaming 我们是怎么进行数据处理的? 批量方式处理 目前最常采用的是批量方式处理,指非工作时间运行,定时或者事件触发.这种方式的好处是逻辑简单,不影响 ...
- Homebrew下安装Cocoapods
在Mavericks下安装Cocoapods遇到ruby安装问题,参照以下网址圆满解决问题. http://www.moncefbelyamani.com/how-to-install-xcode-h ...
- dom树的介绍,及原理分析
三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...
- 【IOS】利用ASIHTTPRequest 实现一个简单的登陆验证
http://blog.csdn.net/toss156/article/details/7638529
- You have new mail in /var/spool/mail/root 烦不烦你?
http://blog.csdn.net/yx_l128125/article/details/7425182
- python学习笔记12(函数三): 参数类型、递归、lambda函数
一.函数参数的类型 之前我们接触到的那种函数参数定义和传递方式叫做位置参数,即参数是通过位置进行匹配的,从左到右,依次进行匹配,这个对参数的位置和个数都有严格的要求.而在Python中还有一种是通过参 ...
- Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二
本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...
- 手写快速排序(QuickSort)
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int ...
- 对象工具类 - ObjectUtils.java
对象工具类,提供对象克隆.获取对象属性.类型判断.Map转换对象.对象转Map.设置对象属性等. 源码如下:(点击下载 - ObjectUtils.java .JsonUtils.java .gso ...