POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可。
每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀。
type arr=record
next:array['a'..'z'] of longint;
w:longint;
end;
const maxn=;
var T:array[..maxn] of arr;
s:array[..maxn] of ansistring;
i,j,m,n,now,num:longint;
begin
while not eof do
begin
inc(n);
readln(s[n]);
end;
num:=;
for i:= to n do
begin
now:=;
for j:= to length(s[i]) do
begin
if T[now].next[s[i][j]]<> then now:=T[now].next[s[i][j]] else
begin
inc(num);
T[now].next[s[i][j]]:=num;
now:=num;
end;
inc(T[now].w);
end;
end;
for i:= to n do
begin
write(s[i],' ');
now:=;
for j:= to length(s[i]) do
begin
now:=T[now].next[s[i][j]];
write(s[i][j]);
if T[now].w= then break;
end;
writeln;
end; end.
POJ2001 Shortest Prefixes (Trie树)的更多相关文章
- poj2001 Shortest Prefixes (trie树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- POJ2001 Shortest Prefixes
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes (trie)
读入建立一棵字母树,并且每到一个节点就增加这个节点的覆盖数. 然后再重新扫一遍,一旦碰到某个覆盖数为1就是这个单词的最短前缀了. 不知为何下面的程序一直有bug……不知是读入的问题? type nod ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- ORACLE数据删除数据删除的解决办法
今天主要以oracle数据库为例,介绍关于表中数据删除的解决办法.(不考虑全库备份和利用归档日志)删除表中数据有三种方法:·delete(删除一条记录)·drop或truncate删除表格中数据 1. ...
- localStorage 读&&写
localStorage.setItem('edit',nowedit); 写 var nowedit1= localStorage.getItem('editdel');读
- php的session
来源:http://blog.163.com/lgh_2002/blog/static/4401752620105246517509/ http协议是WEB服务器与客户 端(浏览器)相互通信的协议,它 ...
- 慕课网6-2 作业:js实现轮播特效
小伙伴们,掌握了JavaScript的语法.流程控制语句.内置对象以及DOM和BOM的知识,运用所学知识完成如下图所示的交互效果——轮播图.效果图如下: 具体交互效果图参考gif动态效果图,gif效果 ...
- 多物体运动框架案例一:多个Div的宽度运动变化
多物体运动框架,鼠标移入Div,此Div逐渐变宽,鼠标移出后,此Div逐渐缩短恢复原长度. <!doctype html> <html> <head> <ti ...
- assistant: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by
[oracle@oracledb button]$ assistantassistant: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' no ...
- MySQL关于存储过程
代码示例: 1.IN输入参数: delimiter // create PROCEDURE proc1(IN sid int) begin select * from student where id ...
- centos mysql允许远程root登录
Mysql为了安全性,在默认情况下用户只允许在本地登录,可是在有此情况下,还是需要使用用户进行远程连接,因此为了使其可以远程需要进行如下操作: 一.允许root用户在任何地方进行远程登录,并具有所有库 ...
- [ AHOI 2008 ] Meet
\(\\\) \(Description\) 一棵\(N\)个节点的树,每条边权都为\(1\). \(M\)组询问,每次给出三个点\(A_i,B_i,C_i\),求从三个点分别出发,移动到同一个点的路 ...
- CSS平滑过渡动画:transition
<html> <head> <link href="http://cdn.bootcss.com/twitter-bootstrap/3.0.2/css/boo ...