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 ...
随机推荐
- ECshop sina
http://blog.sina.com.cn/s/blog_5327408801019ofa.html http://blog.sina.com.cn/u/2624360105 http://blo ...
- ecshop 全站自定义title标题
对于SEO来说,能让标题自定义的将会大大增加SEO效果,提高独立商城的流量,今天小编就收集从网上弄来ecshop全站自定义代码,很全哦! 1.Ecshop商品分类页如何实现自定义Title 最近发现很 ...
- 十一、mysql输入安全
.尽量使用“绑定参数”功能,php中可用pdo进行一系列操作 .php可使用mysql_real_escape_string()函数进行输入过滤:
- sql表连接left join,right join,inner join三者之间的区别
sql表连接left join,right join,inner join区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 (以左表数据为基准,不足补为NULL) ...
- UITableView实现格瓦拉飞天投票模块-b
格瓦拉目前来说动画效果确实做的还比较好,虽然不是说很炫但做到精致,这次就模仿了它投票的模块.其实想到要实现它还是有很多方法,不过这次我还是采用了苹果自带控件UITableView简简单单来实现它,再次 ...
- 1049: [HAOI2006]数字序列 - BZOJ
Description 现在我们有一个长度为n的整数序列A.但是它太不好看了,于是我们希望把它变成一个单调严格上升的序列.但是不希望改变过多的数,也不希望改变的幅度太大.Input 第一行包含一个数n ...
- IE 浏览器下 button元素自动触发click?
为什么这么说呢?且看下面的情形: 当用户焦点在输入框中的时候,当用户按了“Enter”键之后,达到点击登录的效果:所以这里就监听了input的keydown事件,当keyCode是13的时候,就发送请 ...
- hdu 1085
额 母函数 #include <cstdio> #include <cstring> int a[3],b[3]= {1,2,5}; int c1[10001],c2[1 ...
- 关于ref与out的区别
写在最前面 这几天一直在公司接受培训,都是一些基础的知识,同时也乘着这个机会巩固一下自己的基础,基础太重要了.前些时一直看的是多线程方面的知识,接下来我会写一些其他方面的知识,毕竟作为一个实习新人得和 ...
- python参考手册--第2章词汇和语法约定
1.续行符\ 三引号.().{}.[]中的内容不需要续行符 2.空格缩进 优选空格作为缩进,不要用tab,这是因为不同操作系统下tab对应的空格不一样,而python是通过严格的空格来控制语句块的. ...