题目地址: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. asp.net中@page 指令的属性Inherits、Src、CodeBehind区别

    在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. <%@ Page language="c#" C ...

  2. show_space.sql.txt

    create or replace procedure SHOW_SPACE(P_SEGNAME   IN VARCHAR2, P_OWNER     IN VARCHAR2 DEFAULT USER ...

  3. [转] 详细整理:UITableView优化技巧

      原文:http://www.cocoachina.com/ios/20150602/11968.html   最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化 ...

  4. shareSDK微博分享出现: 分享失败: 错误描述:Insufficient app permissions! 错误码:10014

    这个错误是由于appKey所在账号没有微博高级写入接口权限, 需要申请, 详见: http://www.mamicode.com/info-detail-936938.html

  5. 『重构--改善既有代码的设计』读书笔记----Remove Assignments to Parameters

    C++存在按值传递和按引用传递两种传递方式,Java严格按照按值传递这种方式来进行.以按值传递方式的角度来说,如果你 int test(int a) { ) { a = 1; } return a; ...

  6. 关闭iOS的自动更新

    Safari打开网址https://oldcat.me/web/NOOTA9.mobileconfig,安装描述文件,就不会自动下载和提示更新最新的iOS了

  7. 《asp.net mvc3 高级编程》第一章

    以前项目中用过mvc2,虽然mvc4早已出来,但手头只有mvc3的书籍,索性就学学MVC3吧. asp.net mvc 3 概述 (1)友好的试图表达,其中包括新的Razor视图引擎 (2)支持.NE ...

  8. 关于$_SERVER 常量 HTTP_X_FORWARDED_HOST与 HTTP_HOST的问题

    今天在看ecshop的源码,发现了用$_SERVER['HTTP_X_FORWARDED_HOST']来判断主机的地址,就目前来说很多人都是直接通过$_SERVER['HTTP_HOST']来判断的, ...

  9. php cookie不刷新及时生效的实现代码

    <?php /** * 不刷新 cookie及时生效 */ cookie("mycookie","cookievalue",time()+60); coo ...

  10. MySQL笔记--查询语句实践

    有一个用户表,属性为 id,age,buytime 创建表以及插入数据的语句 CREATE TABLE USER( id INT, age INT, buytime INT ) ,,); ,,); , ...