字典树的简单应用。

  

#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的更多相关文章

  1. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

  2. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  3. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  4. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  5. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  6. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  7. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  8. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  9. poj 2001 Shortest Prefixes(特里)

    主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...

  10. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

随机推荐

  1. android分割线

    http://blog.csdn.net/dekunchenivan/article/details/6640804 在Android布局文件layout中设置水平分割线: <View      ...

  2. 逆向x64-small-trick

        在逆向一些x64的代码时,会发现有一小段汇编代码理解不了,这个时候回想,要是能单独执行这一小段汇编就好了.现在我就介绍一个我的解决方法:很小汇编一段代码,但是不好理解,我就把它单独写成了一个函 ...

  3. .NET程序性能优化基本要领

    想了解更多关于新的编译器的信息,可以访问     .NET Compiler Platform ("Roslyn") 基本要领 在对.NET 进行性能调优以及开发具有良好响应性的应 ...

  4. 第一个关于ajax的代码

    昨天由于需要,写了第一个需要ajax的程序,之前只是看过相关介绍,没想到这么有用,记录一下,如有错误,还希望大家提出$(document).ready(function () {//获取url中名字为 ...

  5. (原)css 响应式媒体查询 模板

    @media only screen and (max-width:340px) { html,input{ font-size:80%; } } @media only screen and (ma ...

  6. StringBuffer工具类整理(一)

    package com.gzcivil.utils; /** * 同StringBuffer * * @author Dragon * @time 2013-3-1 */ public class S ...

  7. 用ul、li做横向导航

    /* ul li以横排显示 */ /* 所有class为menu的div中的ul样式 */ div.menu ul { list-style:none; /* 去掉ul前面的符号 */ margin: ...

  8. NOPI 导出excel 通用方法

    public static byte[] ExportExcel<T>(Dictionary<string, string> columnsHeader, List<T& ...

  9. 菜鸟的jQuery源码学习笔记(三)

    each: function(callback, args) { return jQuery.each(this, callback, args); }, each:这个调用了jQuery.each方 ...

  10. Redis深入学习(1)前言&Redis简介

    前言 最近工作上使用到Redis,当然以前也使用过redis,win,linux上都使用过,不系统,不深入,仅是头痛医头,脚痛医脚,这里整理一下自己的笔记,一来方便自己记忆,二来对同行提供借鉴,不足错 ...