poj 2001 Shortest Prefixes
字典树的简单应用。
#include<stdio.h>
#include<string.h> char str[][];
struct node{
int cnt;
node *next[];
}*p;
void build(char str[],int k,node *head)
{
while(k<strlen(str))
{
if(head->next[str[k]-'a']!=NULL)
{
head->next[str[k]-'a']->cnt+=;
head=head->next[str[k]-'a'];
}
else
{
head->next[str[k]-'a']=new node;
head=head->next[str[k]-'a'];
head->cnt=;
for(int i=;i<;i++)
head->next[i]=NULL;
}
k++;
}
}
void search(char str[],int k,node *head)
{
while(k<strlen(str))
{
if(head->next[str[k]-'a']!=NULL)
{
printf("%c",str[k]);
if(head->next[str[k]-'a']->cnt==)
return ;
}
head=head->next[str[k]-'a'];
k++;
}
}
void del(node *head)
{
if(head==NULL)
return ;
for(int i=;i<;i++)
{
del(head->next[i]);
head->next[i]=NULL;
}
delete(head);
}
int main()
{
p=new node;
for(int i=;i<;i++)
p->next[i]=NULL;
int cnt=;
while(~scanf("%s",str[cnt]))
{
build(str[cnt],,p);
cnt++;
}
for(int i=;i<cnt;i++)
{
printf("%s ",str[i]);
search(str[i],,p);
printf("\n");
}
del(p);
return ;
}
poj 2001 Shortest Prefixes的更多相关文章
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- 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(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- HttpContext详解【转】
HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中,会有一个对象在各个对象之间进行传递,也即会保存请求的上下文信息,这个对象就是Htt ...
- SqlHelper数据库访问类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- pixel像素与物理像素
- n条直线的最多交点
#include <iostream>using namespace std;int main(){int i,n;while(cin>>n){if(n==0||n==1) c ...
- php 垃圾回收机制----写时复制和引用计数
PHP使用引用计数和写时复制来管理内存.写时复制保证了变量间复制值不浪费内存,引用计数保证了当变量不再需要时,将内存释放给操作系统. 要理解PHP内存管理,首先要理解一个概念----符号表. 符号表的 ...
- 预定义异常 - PHP手册笔记
Exception是所有异常的基类,类摘要如下: <?php class Exception { protected string $message; // 异常消息内容 protected i ...
- 关于python命令在editor里编写与在interpreter里的编写的不同之处
关于python命令在editor里编写与在interpreter里的编写的不同之处 其实用这个标题,我心里还是有点胆怯的.作为一个python入门的小白,不,编程入门的小白,我还不太确定我对edit ...
- Array 的五种迭代方法 -----every() /filter() /forEach() /map() /some()
ES5定义了五个迭代方法,每个方法都接收两个参数:要在每一项上运行的函数和运行该函数的作用域对象(可选的),作用域对象将影响this的值.传入这些方法中的函数会接收三个参数:数组的项的值.该项在数组中 ...
- 在表单(input)中id和name的区别
但是name在以下用途是不能替代的:1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是全 ...
- JavaScript学习之—prototype
一.利用prototype扩展String方法,去除字符前后空格: String.prototype.trim = function String$trim() { if (arguments.len ...