POJ 2001 Shortest Prefixes(字典树)
题目地址: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(字典树)的更多相关文章
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- OpenJudge/Poj 2001 Shortest Prefixes
1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
随机推荐
- Directory类
string[] pathes = Directory.GetDirectories(@"G:\ReawFiles");//获得所有的文件夹 string[] pathes = D ...
- .net判断用户使用的是移动设备还是PC
using System.Text.RegularExpressions;//头部引入正则的命名空间 //为了加强准确性,防止支持wap的浏览器如opera,加入操作系统验证.openwave|后为p ...
- webservice调用接口,接口返回数组类型
1. 其中sendSyncMsg1接口是方法名,Vector实现了List接口,xml是sendSyncMsg1的方法形参 Service service = new Service(); Call ...
- 使用第三方框架 Masonry 实现自动布局
使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41 阅读:4595 评论:0 收藏:0 [点我收藏+] 标签: 由于前两 ...
- iOS应用架构谈 开篇 (转)
iOS应用架构谈 开篇 iOS应用架构谈 view层的组织和调用方案 iOS应用架构谈 网络层设计方案 iOS应用架构谈 动态部署方案 iOS应用架构谈 本地持久化方案 缘由 之前安居客iOS ap ...
- http请求的cookie
Cookie的作用: Cookie是用于维持服务端会话状态的,通常由服务端写入,在后续请求中,供服务端读取. HTTP请求,Cookie的使用过程 1.server通过HTTP Response中的& ...
- Cloudera Manager(CentOS)安装介绍
相信通过这篇文章大家都对Cloudera Manager及CDH安装有一个整体的认识 目 录 1 准备工 作.................................... ...
- href 里面 链接前面加/与不加的区别?(绝对路径与相对路径)
在写href链接时,有绝对路径与相对路径,href 里面 链接前面加/与不加的区别? href="/cp/images/lis.jpg" 相对路径 cp前面/会获取当前路径,组合成 ...
- WinPcap编程(前言&&学习)
计算机网络课设要求用WinPcap写对ARP包的接收与发送. 所以学了一下WinPcap的内容. 参考的博客: http://blog.csdn.net/htttw/article/details/7 ...
- spring mvc velocity多视图
1.ViewResolverUrlBasedViewResolver 这个东西是根据url 进行路由的.网上搜了 1.order 排序,同名出现各种问题 2.XmlViewResolver,BeanN ...