POJ2001-Shortest Prefixes-Trie树应用
沉迷WOW又颓了两天orz,暴雪爸爸要在国服出月卡了...这是要我好好学习吗?赶紧来刷题了...
OJ:http://poj.org/problem?id=2001
题目大意是求所有字符串里每一个字符串最短唯一前缀(就是说要可以通过这个前缀确定唯一的字符串,也可以理解为这个前缀是某个字符串独有的,求最短的就OK了)
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
;
+;
struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
Trie(){sz=;memset(ch[],,]));memset(val,,sizeof(val));}
int idx(char c){return c-'a';};
void insert(char *s){
/*起点是根节点*/,n=strlen(s);
val[]++;
;i<n;i++){
int x=idx(s[i]);
if(!ch[u][x]){
memset(ch[sz],,sizeof(ch[sz]));
val[sz]++;/*不是最终的节点 附加值是0*/
ch[u][x]=sz++;/*连接起来*/
}else val[ch[u][x]]++;
u=ch[u][x];
}
/*插入完成之后赋值*/
}
void Judge(char *s){
/*从起点开始查找*/,n=strlen(s);
;i<n;i++){
) break;
printf("%c",s[i]);
int x=idx(s[i]);
u=ch[u][x];
}
return;
}
};
][];
Trie book;
int main()
{
,i=;
)){//不能用scanf
N++;
book.insert(input[i-]);
}
;i<N;i++){
printf("%s ",input[i]);
book.Judge(input[i]);
printf("\n");
}
;
}
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 ...
- POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...
- 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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- ASP.NET Eval四种绑定方式
1.1.x中的数据绑定语法 <asp:Literal id="litEval2" runat="server" Text='<%#DataBinde ...
- 黄聪:C#超级延时方法,延迟系统时间但系统又能同时能执行其它任务
private void Delay(int Millisecond) //延迟系统时间,但系统又能同时能执行其它任务: { DateTime current = DateTime.Now; whil ...
- 黄聪:simple_html_dom 换行符丢失
我在利用simple_html_dom来解析文档是,想要将其中的换行符替换成<BR> , 结果试了好几次没有成功,但是在原始文档中确实是有换行符的.后来索性把装载进来的文档打印出来,结果发 ...
- 宏定义中的##操作符和... and _ _VA_ARGS_ _
1.Preprocessor Glue: The ## Operator 预处理连接符:##操作符 Like the # operator, the ## operator can be used i ...
- Shuffle相关分析
Shuffle描述是一个过程,表现出的是多对多的依赖关系.Shuffle是连接map阶段和Reduce阶段的纽带,每个Reduce Task都会从Map Task产生的数据里读取其中的一片数据.Shu ...
- IP地址查询接口
新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...
- 如何利用jQuery进行简单表单验证
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...
- tiny210裸机第1课(启动原理)
软硬件环境 宿主机系统:ubuntu 板子芯片:S5PV210(Contex-A8),512M DDR2,512M SLC Nand 交叉编译器:arm-linux-gcc-4.5.1 手册:S5PV ...
- 为什么选择centos,而不是Dibian、Ubuntu【转】
我们运行着一些全球最大的互联网的营运,因此我们对可靠性和稳定性非常重视,是我们的第一要务.为此,我们只使用Linux来支撑顾客的系统.但是,我们应该使用哪一个Linux的发行版?答案是,CentOS. ...
- 不能使用 snapshot 的解决方式
http://www.mzone.cc/article/654.html 有两种方法可以解决: 1.第一种方法是在项目的pom文件中进行配置,如下: <repositories> < ...