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,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- datagridview 列位置 设置顺序与加载显示顺序不一致
因为: dgv.AutoGenerateColumns = false;//禁止自动生成列 该属性是在 dgvJdmx.DataSource = dt; 之后设置的原因. 将两者调换,即可.
- SpringAOP所支持的AspectJ切点指示器
在spring中尝试使用AspectJ其他指示器时,将会抛出IllegalArgumentException异常. 当我们查看上面展示的这些spring支持的指示器时,注意只有execution指示器 ...
- 外中断之swi软件中断:
在stm32的标准库的外中断库文件中有void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);原来一直不知道有什么用,现总结一下: 作用:软件模拟产生中断能 ...
- CentOS安装apache2(转载)
From:http://www.onepx.com/centos-apache-246.html 之前服务器 Apache 版本一直是 2.2.x,鉴于 Centos 更新软件的惰性,我看直到 201 ...
- .NET 文件相关的所有操作
public static class FileSystemHelper { #region 检测指定目录是否存在 /// <summary> /// 检测指定目录是否存在 /// < ...
- [ActionScript 3.0] AS3 用于拖动对象时一次一页的缓动
package com.fylibs.components.effects{ import com.tweener.transitions.Tweener; import flash.display. ...
- java中的final总结
Java关键字final有最终的,不可改变的含义,它可以修饰非抽象类.非抽象类成员方法和变量. 报错:类"TestFinal"要么是abstract,要么是final的,不能两个都 ...
- Fedora 14配置vsftp服务步骤
Fedora 14配置vsftp服务步骤:1:检查Fedora14是否安装了vsftp服务 用rpm -qa|grep vsftp命令检查是否安装了vsftp服务,如果安装了,会显示安装好的版本 ...
- 如何正确地使用Python的属性和描述符
关于@property装饰器 在Python中我们使用@property装饰器来把对函数的调用伪装成对属性的访问. 那么为什么要这样做呢?因为@property让我们将自定义的代码同变量的访问/设定联 ...
- Codeforces 622F 「数学数论」「数学规律」
题意: 给定n和k,求 1 ≤ n ≤ 109, 0 ≤ k ≤ 106 思路: 题目中给的提示是对于给定的k我们可以求出一个最高次为k+1的关于n的通项公式. 根据拉格郎日插值法,我们可以通过k+2 ...