<题目链接>

题目大意:

找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串。

解题分析:

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树】的更多相关文章

  1. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  2. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  3. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  4. poj 2001 Shortest Prefixes trie入门

    Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...

  5. poj 2001 Shortest Prefixes(字典树)

    题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...

  6. POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15574   Accepted: 671 ...

  7. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  8. POJ 2001 Shortest Prefixes(字典树活用)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21651   Accepted: 927 ...

  9. OpenJudge/Poj 2001 Shortest Prefixes

    1.链接地址: http://bailian.openjudge.cn/practice/2001 http://poj.org/problem?id=2001 2.题目: Shortest Pref ...

随机推荐

  1. sticky footer 模板

    http://www.w3cplus.com/blog/tags/136.html http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-th ...

  2. bzoj 2190

    题意:求 题解:这题...数据范围是真小... 研究一下这一表达式,发现gcd(i,j)=1表示i,j互质,那么互质肯定能想到欧拉函数,可是欧拉函数要求j<i,那么我们变化一下:显然原矩阵是对称 ...

  3. 性能测试四十八:Jenkins+Ant+Jmeter系统部署

    工作步骤: 1.开发提交代码SVN/Git 2.代码编译.打war包 3.上传war包到服务器 4.重启web服务器 5.开始测试/自动化测试 6.发测试结果 Jenkins工作: 1.开发提交代码G ...

  4. 第三周学习总结-Java

    2018年7月29日 这是暑假第三周.这一周我把找到的Java教学视频看完了. 本周学到了Java剩余的基础知识,比如:抽象类.接口.内部类.几种常用类.IO流.多态.多线程等等. 因为没有书,所以我 ...

  5. Java 单字节、多字节读取文本文档中的内容

    文本文档位于工程下. 鼠标右击工程,选择“new - File”,即可创建. 文本文档的格式:GBK 单字节读取 import java.io.File; import java.io.FileInp ...

  6. tomcat启动报错:Annotation-specified bean name 'patrolTrailServiceImpl' for bean class [cn.oppo.inventoryService.patrolTrailServiceImpl] con

    注释-为bean类[目录服务patrolTrailServiceImpl]指定的bean名称“patrolTrailServiceImpl”与同名和[目录服务patrolTrailServiceImp ...

  7. ubuntu下使用matplotlib绘图无法显示中文label

    原因是字体导致的.大家的做法基本都是搞一个windows上的字体文件(simhei.ttf, 点我fq下载)然后刷新一下缓存文件. 只不过百度搜到第一篇CSDN的博客,写的很不靠谱(不是所有的CSDN ...

  8. jquery表单提交的新写法

    $('form').submit()和$("form").submit() 这两种都可以实现form表单的提交 jquery中$('form').submit()和$(" ...

  9. rxjs简单入门

    rxjs全名Reactive Extensions for JavaScript,Javascript的响应式扩展, 响应式的思路是把随时间不断变化的数据.状态.事件等等转成可被观察的序列(Obser ...

  10. squid,nginx,lighttpd反向代理的区别

    反向代理从传输上分可以分为2种: 1:同步模式(apache-mod_proxy和squid) 2:异步模式(lighttpd 和 nginx) 在nginx的文档说明中,提到了异步传输模式并提到它可 ...