读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数。

然后再重新扫一遍,一旦碰到某个覆盖数为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)的更多相关文章

  1. POJ2001 Shortest Prefixes (Trie树)

    直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...

  2. Shortest Prefixes(trie树唯一标识)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15948   Accepted: 688 ...

  3. poj2001Shortest Prefixes(trie)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 18687   Accepted: 808 ...

  4. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

  5. Leetcode 943. Find the Shortest Superstring(DP)

    题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...

  6. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  7. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  8. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  9. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

随机推荐

  1. 爬虫学习之基于Scrapy的网络爬虫

    ###概述 在上一篇文章<爬虫学习之一个简单的网络爬虫>中我们对爬虫的概念有了一个初步的认识,并且通过Python的一些第三方库很方便的提取了我们想要的内容,但是通常面对工作当作复杂的需求 ...

  2. mac 下 配置 xhprof

    1: 下载 安装 xhprof wget http://pecl.php.net/get/xhprof-0.9.3.tgztar zxf xhprof-0.9.3.tgzcd xhprof-0.9.3 ...

  3. DataSet数据导出为Excel文档(每个DataTable为一个Sheet)

    Web项目中,很多时候须要实现将查询的数据集导出为Excel文档的功能,很多时候不希望在工程中添加对Office组件相关的DLL的引用,甚至有时候受到Office不同版本的影响,导致在不同的服务器上部 ...

  4. Xcode 合并分支报错

    原理和操作步骤见如下转载的两篇文章, 我所使用的 svn 客户端软件是 Mac 下面的 Versions.app v1.06 这个版本包含一个多人开发的bug bug 的解决方案见我之前转载的两篇文章 ...

  5. C#和Js 编码和解码方法

    Server.UrlDecode(); Server.UrlEncode(); Server.HtmlDecode(); Server.HtmlEncode();

  6. ./configure详解

    'configure'脚本有大量的命令行选项.对不同的软件包来说,这些选项可能会有变化,但是许多基本的选项是不会改变的.带上'--help'选项执行'configure'脚本可以看到可用的所有选项.尽 ...

  7. Spark的TorrentBroadcast:实现

    依据Spark 1.4版 序列化和反序列化 前边提到,TorrentBroadcast的关键就在于特殊的序列化和反序列化设置.1.1版的TorrentBroadcast实现了自己的readObject ...

  8. IDS 日志分析

    [http://blog.csdn.net/cnbird2008/article/details/5792626] General Approach通用方法1. Identify which log ...

  9. 扩展DJANGO的LISTVIEW

    不用MODEL,不用QUERYSET,而用get_queryset方法来扩展LISTVIEW, 从而实现特定过滤或搜索功能. class DVListView(ListView): template_ ...

  10. Android 显示大图片

    主要的代码如下: BitmapFactory.Options options = new BitmapFactory.Options(); //图片解析配置 options.inJustDecodeB ...