题目地址:POJ 2001

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

代码例如以下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm> using namespace std;
char s[1100][30];
struct node
{
int flag;
node *next[30];
};
node *newnode()
{
int i;
node *p;
p=new node;
for(i=0;i<26;i++)
{
p->next[i]=NULL;
}
p->flag=0;
return p;
}
void insert1(node *root, char *s)
{
int i, len=strlen(s), x;
node *p=root;
for(i=0;i<len;i++)
{
x=s[i]-'a';
if(p->next[x]==NULL)
p->next[x]=newnode();
p=p->next[x];
p->flag++;
}
}
int seach(node *root, char *s)
{
int i, len=strlen(s), x, pos=len;
node *p=root;
for(i=0;i<len-1;i++)
{
x=s[i]-'a';
p=p->next[x];
if(p->flag==1)
{
pos=i+1;
break;
}
}
return pos;
}
int main()
{
node *root;
root =newnode();
int cnt=0, i, j, k;
while(scanf("%s",s[cnt])!=EOF)
{
insert1(root,s[cnt]);
cnt++;
}
for(i=0;i<cnt;i++)
{
printf("%s ",s[i]);
k=seach(root,s[i]);
for(j=0;j<k;j++)
{
printf("%c",s[i][j]);
}
printf("\n");
}
return 0;
}

POJ 2001 Shortest Prefixes(字典树)的更多相关文章

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

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

  2. poj 2001 Shortest Prefixes(字典树)

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

  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 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

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

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

  6. OpenJudge/Poj 2001 Shortest Prefixes

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

  7. POJ 2001 Shortest Prefixes (Trie)

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

  8. poj 2001 Shortest Prefixes trie入门

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

  9. poj2001 Shortest Prefixes(字典树)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21642   Accepted: 926 ...

随机推荐

  1. 一.去除字符串中的html标记及标记中的内容

    --1.创建函数 )) ) as begin declare @i int begin set @i=len(@maco) set @maco=replace(@maco, substring(@ma ...

  2. 困扰:C#.net 连接Oracle11g 不报错但是在connection时出现 ServerVersion 引发了“System.InvalidOperationException”类型的异常

    今天在使用VS2008 32位 连接 64位的Oracle11g的数据库时出现 “conn.ServerVersion”引发了“System.InvalidOperationException”类型的 ...

  3. Linux硬盘命名和安装分区

    硬盘命名: 硬盘命名基于文件,一般有如下文件方式: /dev/hda1 /dev/sdb3 具体含义如下: /dev:是所有设备文件存放的目录. hd和sd:他们是区别的前两个字母,代表该分区所在的设 ...

  4. javascript之attribute 和 property

    首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...

  5. sublime常用插件及配置,自留自用

    1.Angularjs 写angularjs经常操作template文件,没有一个ng-xx的提示真的很蛋疼是不是,有些服务的名字记不住是不是,那就用这个插件吧 2.AutoFileName 如果你的 ...

  6. html中插入flash代码详解(转载)

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  7. PHP程序异常处理实现方法

    一个异常(Exception)则是在一个程序执行过程中出现一个例外或是一个事件,它中断了指令的运行,跳转到其他程序模块继续执行.所以异常处理经常被当作程序的控制流程使用.无论是错误还是异常,应用程序都 ...

  8. linux 路由表设置 之 route 指令详解

    使用下面的 route 命令可以查看 Linux 内核路由表. # route Destination     Gateway         Genmask Flags Metric Ref     ...

  9. 把图片生成Base64字符串

    public class ImgeUtils { public static String img2String(BufferedImage img,String type){ String imgS ...

  10. Windows平台下主要的内存管理途径

    new / delete   malloc / free    CoTaskMemAlloc / CoTaskMemFree    IMalloc::alloc / IMalloc/free    G ...