POJ 2001 Shortest Prefixes(字典树入门)

2024-09-07 18:08

本文主要是介绍POJ 2001 Shortest Prefixes(字典树入门),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

http://poj.org/problem?id=2001

题意:

找到多个字符串中,各自唯一的最短子串,例如 carte 与 carce 各自唯一的最短子串为cart与carc,即不会与其它字符串的子串重复。

思路:

字典树

解题心得:

更新关键值的时机很重要

代码:

#include<stdio.h>
#include<string>
typedef struct Node
{char c;int count;struct Node* next[26]; 
}Node;
Node* CreateNode()
{Node* T=(Node*)malloc(sizeof(Node));int i;for(i=0;i<26;i++){T->next[i]=NULL;}T->count=0;return T;
}
void insert(Node* T,char* str,int x)
{int len=strlen(str);//1.判断树根下是否存在st[i]int i;for(i=0;T->next[i]!=NULL;i++){//存在,更新count,并继续往下搜索。if(T->next[i]->c==str[x]){T->next[i]->count++;x++;//字符串结束if(x==len){return;}else{insert(T->next[i],str,x);return;}}}//2.不存在则建立节点if(T->next[i]==NULL){Node* temp=CreateNode();temp->c=str[x];temp->count=1;T->next[i]=temp;x++;if(x==len) return;else{insert(T->next[i],str,x);return;}}
}
void print(Node* T,char* str,int x)
{//寻找T下,关键值为str[x]的节点int i;int len=strlen(str);for(i=0;T->next[i]!=NULL;i++){if(T->next[i]->c==str[x]){if(T->next[i]->count==1){printf("%c",T->next[i]->c);return;}else{printf("%c",T->next[i]->c);x++;if(x<len) {print(T->next[i],str,x);}return;}}}
}
int main()
{char str[1001][30];Node* Tree=CreateNode();int i=0;while(gets(str[i])){Tree->count++;insert(Tree,str[i],0);i++;}int j;for(j=0;j<i;j++){printf("%s ",str[j]);print(Tree,str[j],0);printf("\n");}return 0;
}


这篇关于POJ 2001 Shortest Prefixes(字典树入门)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145776

相关文章

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python容器类型之列表/字典/元组/集合方式

《Python容器类型之列表/字典/元组/集合方式》:本文主要介绍Python容器类型之列表/字典/元组/集合方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 列表(List) - 有序可变序列1.1 基本特性1.2 核心操作1.3 应用场景2. 字典(D

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

python 字典d[k]中key不存在的解决方案

《python字典d[k]中key不存在的解决方案》本文主要介绍了在Python中处理字典键不存在时获取默认值的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录defaultdict:处理找不到的键的一个选择特殊方法__missing__有时候为了方便起见,

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n