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 ...
随机推荐
- collections模块
collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.defaultdict.named ...
- cf1131f 构造+并查集
#include<bits/stdc++.h> using namespace std; #define maxn 150005 int f[maxn],n; vector<int& ...
- poj1185 状态压缩经典题
状态压缩的好题,直接求会爆内存,先把所有可能的状态求出来存在stk里,然后f[i][k][t]表示i行状态为t,i-1状态为k,由i-1状态来推出i状态即可 注意要打好边际条件的状态,并且某个可行状态 ...
- css+js杂记
css的盒子模型分:ie盒子模型和标准盒子模型 区别: 2.选择器 .id选择器(# myid) .类选择器(.myclassname) .标签选择器(div, h1, p) .相邻选择器(h1 + ...
- spring cloud Hystix熔断机制--基本熔断配置和Fegin client熔断配置
所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的提供方出现了问题之后整个的程序将出现错误的信息显示 ...
- JS判断元素 动画是否执行完成
使用animationend方法 var ele = document.getElementById("box"); ele.addEventListener("anim ...
- VOC数据集生成代码使用说明
#split.py 文件 输入格式为images ,和标签txt文件,txt中的数据为坐标值共8个. import os import numpy as np import math import c ...
- Django主线
Django怎么学: 参考地址:https://www.zhihu.com/question/26235428 需要了解的知识点: Django Url请求流程 首要操作 Django的安装 pip3 ...
- linux 出错 “INFO: task xxxxxx: 634 blocked for more than 120 seconds.”的3种解决方案
https://blog.csdn.net/electrocrazy/article/details/79377214
- ASP.NET CORE 配置管理
配置管理简单例子(添加内存配置) using Microsoft.Extensions.Configuration; using System; using System.Collections.Ge ...