字典树基本题。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib> using namespace std;
#define N 1027 struct node
{
int count;
node *next[];
}*root; char ss[N][]; node *create()
{
node *p;
p = (node *)malloc(sizeof(node));
p->count = ;
for(int i=;i<;i++)
p->next[i] = NULL;
return p;
} void release(node *p)
{
for(int i=;i<;i++)
{
if(p->next[i] != NULL)
release(p->next[i]);
}
free(p);
} void insert(char *ss)
{
int i=,k;
node *p = root;
while(ss[i])
{
k = ss[i++] - 'a';
if(p->next[k] == NULL)
p->next[k] = create();
else
p->next[k]->count++;
p = p->next[k];
}
} void search(char *ss)
{
int i = ,k,j,flag = ;
node *p = root;
while(ss[i] && !flag)
{
k = ss[i++] - 'a';
p = p->next[k];
if(p->count == )
{
flag = ;
printf("%s ",ss);
for(j=;j<i;j++)
printf("%c",ss[j]);
printf("\n");
}
}
if(!flag)
printf("%s %s\n",ss,ss);
} int main()
{
int i=,j,k;
root = create();
while(scanf("%s",ss[i])!=EOF)
{
insert(ss[i]);
i++;
}
for(j=;j<i;j++)
search(ss[j]);
release(root);
return ;
}

POJ 2001 Shortest Prefix的更多相关文章

  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(特里)

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

  3. POJ 2001 Shortest Prefixes (Trie)

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

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

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

  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(字典树)

    Description A prefix of a string is a substring starting at the beginning of the given string. The p ...

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

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

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

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

随机推荐

  1. mybatis中#和$符号的区别

    mybatis做为一个轻量级ORM框架在许多项目中使用,因其简单的入门受到了广大开发者的热爱.在近期项目中再做一个相关的开发,碰到了#.$符号这样的问题,之前没怎么注意过,通过学习之后,有了点感悟,分 ...

  2. D/A转换器

    电荷:带正负电的基本粒子.电的本质是使正负电荷分开,使电荷发生移动,实质是电子的转移,并不是创造电荷.电压:单位正电荷受电场力作用从A点移动到B点所做的功.电压方向从高电位指向低点位.电压是推动电荷定 ...

  3. CSS类选择器和ID选择器

    CSS类选择器和ID选择器皆允许以一种独立于文档元素的方式来指定样式,同时二者皆区分大小写. 区别如下: 第一:在同一个页面内,不允许有相同名字的id对象出现,但是允许相同名字的class 第二:当页 ...

  4. Button 对 TreeView1 所有节点的全选

    protected void Button1_Click(object sender, EventArgs e)    {        for (int i = 0; i < this.Tre ...

  5. IOS异常日志记录与展现功能

    在平常的APP开发过程中经常碰到程序遇到异常闪退的问题,通过日志可以把相关的详细错误信息进行记录,本实例要记录不管在哪个页面出错都要进行记录,这边使用到的日志记录插件CocoaLumberjack,以 ...

  6. 【原】训练自己haar-like特征分类器并识别物体(1)

    本系列文章旨在学习如何在opencv中基于haar-like特征训练自己的分类器,并且用该分类器用于模式识别.该过程大致可以分为一下几个大步骤: 1.准备训练样本图片,包括正例及反例样本 2.生成样本 ...

  7. 多线程之NSOperation和NSOperationQueue

    这篇文章里我将不过多的谈及理论知识,这些东西会的自然会,不会的,看多了也是云里雾里.下面我讲更多的用代码+注释的方式来讲如何使用NSOperation和NSOperationQueue. 1.NSOp ...

  8. iOS之 随笔Xcode7的lipo

    此文是学习所用,若要转载请注明出处 Xcode的lipo xcrun -sdk iphoneos lipo Xcode7以后lipo存放位置变化 /Applications/Xcode.app/Con ...

  9. iOS:交换Button中图片与文字的左右位置

    titleEdgeInsets属性和 imageEdgeInsets属性只是在画这个button出来的时候用来调整image和label位置的属性,并不影响button本身的大小.它们只是image和 ...

  10. Git+GitHub 使用小结

    1.Git安装完成后需要做的配置            $ git config --global user.name "Your Name"        $ git confi ...