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 ...
随机推荐
- Swift Write to file 到电脑桌面
(arr as NSArray).write(toFile: "Users/你的用户名/Desktop/mian.plist", atomically: true)
- python自动化-unittest批量执行用例(discover)
前言 我们在写用例的时候,单个脚本的用例好执行,那么多个脚本的时候,如何批量执行呢?这时候就需要用到unittet里面的discover方法来加载用例了. 加载用例后,用unittest里面的Text ...
- 二.Rsync备份服务
自己动手部署一遍 期中架构-第二章-备份服务笔记====================================================================== 01. 课 ...
- 【linux】安装python依赖库confluent_kafka
想跑https://github.com/ghaughian/mongo-kafka-spark/blob/master/src/pub.py这个程序,发现没有confluent_kafka库 1.p ...
- laravel Blade 模板引擎
与视图文件紧密关联的就是模板代码,我们在视图文件中通过模板代码和 HTML 代码结合实现视图的渲染.和很多其他后端语言不同,PHP 本身就可以当做模板语言来使用,但是这种方式有很多缺点,比如安全上的隐 ...
- cf842C 树形dp+gcd函数
树形dp用一下就好了 /* dp[i]表示不删节点的gcd值 每个结点开个vector用来存储删一个点之后的最大值 然后排序 去重 */ #include<bits/stdc++.h> # ...
- Python集合(set)
Python中的集合同数学中的集合概念类似,也是用于保存不重复的元素.他有可变集合(set),和不可变集合(frozenset);可变集合(set)是无序的可变的. 创建集合 直接使用{}创建 set ...
- 无废话-API-01
说明 我的开发环境:VS2013 浏览器:谷歌浏览器(Google Chrome) 1创建项目 1.1添加一个 应用程序"ASP.NET MVC 4 Web 应用程序" 1.2选 ...
- k3 Bos开发百问百答
K/3 BOS开发百问百答 (版本:V1.1) K3产品市场部 目录 一.基础资料篇__ 1 [摘要]bos基础资料的显示问题_ 1 [摘要]单 ...
- node.js vue-axios和vue-resource
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...