POJ 2001 Shortest Prefixes 【Trie树】
<题目链接>
题目大意:
找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串。
解题分析:
Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 ,这样在查询的时候,如果遍历到num值为1的节点,就可以确定,该前缀能够唯一确定一个字符串,或者是一直遍历到NULL,这时直接输出它本身即可。
指针式Trie树:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N =1e3+;
char word[N][];
struct Node{
int num;
Node *next[];
Node(){
num=;
for(int i=;i<;i++)
next[i]=NULL;
}
};
Node *root;
Node *now,*newnode;
void Insert(char *str){
now=root;
for(int i=;i<strlen(str);i++){
int to=str[i]-'a';
if(now->next[to]==NULL){ //如果该节点为空
newnode=new Node;
++(newnode->num);
now->next[to]=newnode;
now=now->next[to];
}
else{
now=now->next[to];
++(now->num);
}
}
}
void Search(char *str){
char ans[];
now=root;
for(int i=;i<strlen(str);i++){
int to=str[i]-'a';
now=now->next[to];
ans[i]=str[i]; //ans[]记录下前缀字符串
ans[i+]='\0';
if(now->num==){ //如果该节点只有一个对应的公共前缀,那么就是它本身的前缀,所以直接输出该节点的对应前缀即可
printf("%s %s\n",str,ans);
return;
}
}
printf("%s %s\n",str,str);
}
int main(){
root=new Node;
int cnt=;
while(gets(word[++cnt])){
//if(!strlen(word[cnt]))break;
Insert(word[cnt]);
}
for(int i=;i<=cnt;i++)Search(word[i]);
return ;
}
数组式Trie树 转载于 >>>
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char a[][];
int tot,size;
int sz,t[][],s[];
void insert(char ch[])
{
int k,len=strlen(ch+),now=;
for(int p=;p<=len;p++)
{
k=ch[p]-'a';
if(t[now][k]==)t[now][k]=++sz;
now=t[now][k];
s[now]++;
}
}
void ask(char ch[])
{
int now=,k,len=strlen(ch+);
for(int p=;p<=len;p++)
{
if(s[now]==)break;
k=ch[p]-'a';
printf("%c",ch[p]);
now=t[now][k];
}
}
int main()
{
while(scanf("%s",a[++tot]+)!=EOF)insert(a[tot]);
for(int i=;i<=tot;i++)
{
printf("%s ",a[i]+);
ask(a[i]);
printf("\n");
}
return ;
}
2018-10-29
POJ 2001 Shortest Prefixes 【Trie树】的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- 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 ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
- 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: 12731 Accepted: 544 ...
- 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 ...
随机推荐
- nikto for windows(web扫描工具) 使用教程
本文出处: 欧普软件 ----------------------------------------------------------------------------------------- ...
- centos7 nginx图片 服务器可以访问ftp用户上传的图片资源的配置
注:本文参考了csdn:JAVA_DIRECTION的<nginx和ftp搭建图片服务器>一文.在实践中其文在centos7中还是存在缺陷性的 一:前提条件:是成功的安装好了ftp服务器和 ...
- 从 Confluence 5.3 及其早期版本中恢复空间
如果你需要从 Confluence 5.3 及其早期版本中的导出文件恢复到晚于 Confluence 5.3 的 Confluence 中的话.你可以使用临时的 Confluence 空间安装,然后将 ...
- 【JS】中ajax的URL中包含中文,后台接收乱码
[问题]ajax提交get请求,url中参数包含中文,后台接收到显示乱码. [解决方案]前台: function getSiteInfoByName(siteName){ var res; $.aja ...
- poj2441状态压缩dp基础
/* 给定n头牛,m个谷仓,每头牛只能在一些特定的谷仓,一个谷仓只能有一头牛 问可行的安排方式 dp[i][j]表示前i头牛组成状态j的方案数,状态0表示无牛,1表示有牛 使用滚动数组即可 枚举到第i ...
- mysql登录报错:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
在MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问 对于出现拒绝访问root用户的解决方案错 ...
- C语言将字符串转json
示例代码: #include <stdio.h> #include <string.h> #include <stdlib.h> char *strrpc(char ...
- ThreadLocal用法详解和原理
一.用法 ThreadLocal用于保存某个线程共享变量:对于同一个static ThreadLocal,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量. 1.Thr ...
- 微信小程序 Video默认横屏
wxml文件 <video id='myvideo' src='你的视频文件路径'> </video> js文件 onLoad: function (options) { va ...
- Mongodb for .Net Core 封装类库
一:引用的mongodb驱动文件版本为 Mongodb.Driver 20.4.3 二:我只是进行了常用方法的封装,如有不当之处,请联系我 创建mongodb的连接 using MongoDB.Bso ...