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 ...
随机推荐
- .net mvc 防止 xss 与 CSRF
CSRF(Cross-site request forgery跨站请求伪造,也被称成为“one click attack”或者session riding,通常缩写为CSRF或者XSRF,是一种对网站 ...
- CCNP第一天 Rip综合实验
实验题如图所示 R2--R6之间是RipV2, R7和R8是RipV1, 除了R2到R6是快速以太网线,其他均为串线. 所用的拓扑图为CCNP标准版拓扑 如下: ------------------- ...
- Python入门二:函数
一.函数的定义和使用 1.基本结构: def 函数名(参数): """ 文档字符串 """ 函数体 返回值 2.函数名: 和变量名命名规则一 ...
- ISBN
问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...
- Kinetic使用注意点--image
new Image(config) 参数: config:包含所有配置项的对象. { image: "图片对象", crop: "图片裁剪对象", fill: ...
- php源代码安装常见错误与解决办法分享
错误:configure: error: libevent >= 1.4.11 could not be found 解决:yum -y install libevent libevent-de ...
- 泛形集合List<T>
public class Person { /// <summary> /// 姓名 /// </summary> private string name; public st ...
- HTTP POST GET 区别
一 原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Http定义了与服务器交互的不同方法,最基本的 ...
- scrum敏捷开发
团队PM:袁佩佩 scrum敏捷开发计划制定: 确定项目实施具体阶段目标 确定项目相关任务分解 确定每日站立会议进行计划 确定项目计划总结日程 确定风险解决方案
- PAT-乙级-1029. 旧键盘(20)
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...