题目地址: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. javascript innerHTML、outerHTML、innerText、outerText的区别

    1.功能讲解: innerHTML 设置或获取位于对象起始和结束标签内的 HTML outerHTML 设置或获取对象及其内容的 HTML 形式 innerText 设置或获取位于对象起始和结束标签内 ...

  2. xls和xlsx

      xls XLS 就是 Microsoft Excel 工作表,是一种非常常用的电子表格格式.xls文件可以使用Microsoft Excel打开,另外微软为那些没有安装Excel的用户开发了专门的 ...

  3. Character Encoding tomcat.

    default character encoding of the request or response body: If a character encoding is not specified ...

  4. 【转】NSString / NSData / char* 类型之间的转换

    原文 :http://www.cnblogs.com/pengyingh/articles/2341880.html NSString 转换成NSData 对象 NSData* xmlData = [ ...

  5. Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view...

    Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tabl ...

  6. JavaScript_object(基于map和数组练习)

    JavaScript在编程过程中,不会像java那么方便,提供大量的API让开发这直接去调用,但底层也封装大量的工具让开发者写方法去实现, 以map为例,写简单的增删改查模拟java中的map: fu ...

  7. WebService笔记-Schema约束

                        Schema约束 俗话说得好 好记性不如烂笔头. 看了下WebService视频,觉得还是得下笔记. 观看的视频地址:http://edu.51cto.com/ ...

  8. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. 【USACO 3.1.2】总分

    [描述] 学生在我们USACO的竞赛中的得分越多我们越高兴.我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助.我们可以从几个种类中选取竞赛的题目,这里的一个"种类"是 ...

  10. 不同浏览器对parseInt方法解析的不同

    parseInt方法的作用是将字符串转换为数字 当parseInt解析的时候只有0x和非0开头的数字,浏览器解析都一致,例如”0xA1”或 “9”. 只有当开头为0的时候才出现不同.IE,chrome ...