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 ...
随机推荐
- XZ压缩最新压缩率之王
xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 我是在下载phpmyadmin的时候看到这种压缩格式的,phpm ...
- vb6-很简单的配置密码验证提示
'很简单的配置密码验证提示 Dim add As String add = Trim(InputBox("请输入配置密码", "报表配置")) If add = ...
- 【转】How to view word document in WPF application
How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...
- WPF中利用后台代码实现窗口分栏动态改变
在WPF中实现窗口分栏并能够通过鼠标改变大小已经非常容易,例如将一个GRID分成竖排三栏显示,就可以将GRID先分成5列,其中两个固定列放GridSplitter. <Grid Backgrou ...
- mac 下 sphinx + mysql + php 实现全文搜索(xampp)(4)php api 解析
1:function GetLastError() // 假如报错的话,会输出报错信息 2:function GetLastWarning ()// 输出 警告信息 3:function SetSe ...
- ORA-19809: 超出了恢复文件数的限制
实验环境:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod 实验背景:向tough.t中插入40万条记录,然后rollb ...
- Import和SQL*Loader这2个工具的异同
问:请讲述Import和SQL*Loader这2个工具的异同? 解答: 相同点:这两个ORACLE工具都是用来将数据导入数据库的. 区别是: IMPORT工具只能处理由另一个ORACLE工具EXPOR ...
- Arcgis投影变换后图像变深的问题
首先投影时,重采样方式选择nearest最邻近采样法,不改变投影后图像的DN值。 然后双击投影后图像,在符号系统中选择RGB——拉伸类型改为:无(默认是标准差拉伸)
- php使用注意点
php使用时间之前要将php.ini中时区设置好,否则会报警告.截图如下:“;date.timezone =”设置为“date.timezone =Asia/Shanghai”即可. apache如果 ...
- nginx低版本不支持pathinfo模式,thinkphp针对此问题的解决办法
将一个thinkphp项目从apache环境移到nginx1.2上,怎奈,nginx这个版本默认不支持pathinfo模式 首先,编辑nginx的虚拟主机配置文件 location ~ .*.(php ...