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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- 2. scala中的数组
一. 数组声明 定长数组 scala> new Array[String](5) res1: Array[String] = Array(null, null, null, null, null ...
- POJ 1269 Intersecting Lines(计算几何)
题意:给定4个点的坐标,前2个点是一条线,后2个点是另一条线,求这两条线的关系,如果相交,就输出交点. 题解:先判断是否共线,我用的是叉积的性质,用了2遍就可以判断4个点是否共线了,在用斜率判断是否平 ...
- shell中实现自动登录(bash环境脚本中)
自己的脚本: #!/bin/bash expect -c " set timeout 3600; spawn su -; expect *assword:*; ...
- 转__Android Studio ,基于intellij idea
看到论坛里一些关于Android Studio的帖子,基本上是停留在使用教程上.在此做一些功能性的分析和测评 下载地址 :http://developer.android.com/index.html ...
- centos7 安装中文编码
运行如下 yum groups mark install "Chinese Support"
- Mac下lombok无法安装到eclipse mars
eclipse升级到mars之后 , 在mac下已经不再是文件夹中有很多文件的eclipse了 , 只有一个单独的app文件.用原来的方式运行lombok再选eclipse.app已经不行了. 自己鼓 ...
- Jetty和Tomcat的使用及性能测试
一 测试目的 这次对Jetty和Tomcat进行性能测试,主要是为了给新版本WebPortal的开发选择合适的Java Web Server. 我们之前对老的Rest和新的TMMI都进行过性能测试,R ...
- http请求的referer属性
HTTP Referer是header的一部分,当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器籍此可以获得一些信息用于处理.比如从我主页上链 ...
- ICE安装
第一步,基于Windows下的安装,所以下载windows版的Ice: http://www.zeroc.com/download 第二步,安装Ice: 常规安装即可,可以选择安装目录,本 ...
- Sql Server Text 类型列 查询和更新
Text(ntext.image)类型为大数据字段,因为存储方式不同,也决定了其查询和更新不同于一般方法. 1.表定义: 2.查询: Like查询是可用的: select * from dbo.nod ...