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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- [Apple开发者帐户帮助]九、参考(5)支持的功能(tvOS)
tvOS应用程序可用的功能取决于您的程序成员身份. 能力 ADP 企业 Apple开发者 应用程序组 相关域名 背景模式 数据保护 游戏中心 游戏控制器 HomeKit iCloud:Cl ...
- Mobile
模块===包 传统开发:整个网页我们写了一个js文件,所有的特效都写在里面了. 缺点:耦合度太高,代码关联性太强,不便于后期维护,会造成全局污染. 发生的请求次数过多,依赖模糊,难于维护. 以上都 ...
- Akka源码分析-ask模式
在我之前的博文中,已经介绍过要慎用Actor的ask.这里我们要分析一下ask的源码,看看它究竟是怎么实现的. 开发时,如果要使用ask方法,必须要引入akka.pattern._,这样才能使用ask ...
- webview加载本地页面
main.xml中布局webview,activity中设置如下 MyWebView=(WebView)findViewById(R.id.webView1); MyWebView.requestFo ...
- [转]linux之at指令详解
转自:http://www.2cto.com/os/201409/336183.html 指令:at定时任务,指定一个时间执行一个任务,只能执行一次. 语法:# at [参数] [时间]at> ...
- Sql Server 如何解决多并发情况下,出现的多个相同ID数据
在数据库中单独创建一张表,保存当前存储状态,“存储过程” 设置访问条件root初始值为“0” 如果root值不为0的时候就不可访问并进行相关操作. 在事务执行前将root值设置为1,事务结束后将ro ...
- JavaScript中的 函数splice() 的使用。
大二接触JavaScript初期,学习函数中有一道题: 定义一个2个参数的函数.第1个参数是一个数组,第2个参数是需要删除的元素.函数功能,在第1个实参数组中查找第2个实参提供的值,找到则删除该元素( ...
- Json解析与Gson解析
本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...
- 【PostgreSQL-9.6.3】触发器概述(普通触发器)
一个触发器声明了当执行一种特定类型的操作时数据库应该自动执行一个特殊的函数.触发器可以被附加到表.视图和外部表.触发器经常用于做完整性约束,或者某种业务规则的约束. 1. 触发器的创建语法如下: CR ...
- MariaDB常用命令手记
创建用户命令 mysql>create user username@localhost identified by 'password'; 直接创建用户并授权的命令 mysql>grant ...