POJ2001 Shortest Prefixes【字典树】
来源:互联网 发布:汽车租赁源码 编辑:程序博客网 时间:2024/05/17 07:09
题目链接:
http://poj.org/problem?id=2001
题目大意:
给一些字符串,求出每个字符串在这些字符串中能可以被唯一识别的最小前缀。
思路:
字典树的模板题,结构体中庸Count来存储字符前缀出现的次数,字典树中找到第一个
Count为1的前缀时,该前缀就是被唯一识别的最小前缀。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;struct TrieNode{int Count;struct TrieNode *Next[26];};TrieNode *root;void Create(){root = new TrieNode;memset(root->Next,NULL,sizeof(root->Next));root->Count = 0;}void Insert(char *s){TrieNode *p, *q;p = root;while(*s){if(p->Next[*s-'a'] == NULL){q = new TrieNode;memset(q->Next,NULL,sizeof(q->Next));q->Count = 1;p->Next[*s-'a'] = q;}elsep->Next[*s-'a']->Count++;p = p->Next[*s-'a'];s++;}}void Find(char *s){TrieNode *p, *q;p = root;while(*s){if(p->Count == 1)break;printf("%c",*s);p = p->Next[*s-'a'];s++;}printf("\n");}char word[1100][22];int main(){int k = 0;Create();while(scanf("%s",word[k]) != EOF){Insert(word[k]);k++;}for(int i = 0; i < k; ++i){printf("%s ",word[i]);Find(word[i]);}return 0;}
0 0
- POJ2001 - Shortest Prefixes - 字典树
- POJ2001 Shortest Prefixes【字典树】
- 【poj2001】Shortest Prefixes 字典树
- POJ2001 Shortest Prefixes(字典树)
- POJ2001 Shortest Prefixes(字典树)
- kyeremal-poj2001-Shortest Prefixes-字典树trie
- POJ2001 Shortest Prefixes 动态字典树实现
- [复习][poj2001]字典树(trie树)Shortest Prefixes
- 字典树trie树 模板题 POJ2001 Shortest Prefixes
- poj2001 Shortest Prefixes (trie树)
- poj2001 Shortest Prefixes trie树
- 【Trie树】POJ2001 Shortest Prefixes
- POJ2001 Shortest Prefixes trie树模板
- Shortest Prefixes(poj2001,tire树)
- POJ2001 Shortest Prefixes
- poj2001 Shortest Prefixes
- 【poj2001】Shortest Prefixes
- POJ2001--Shortest Prefixes
- PL/SQL ORA-12154: TNS: 无法解析指定的连接标识符
- leetcode || 90、Subsets II
- iOSJsonKit的使用
- 12.1 抵达鱼人岛
- vmware 提示该虚拟机正在使用中
- POJ2001 Shortest Prefixes【字典树】
- C++算法之 合并两个有序链表
- 内容提供者创建,访问和权限
- Openstack Nova(七)----Instance 创建(nova WSGI)
- BZOJ1925【动态规划】【组合数学】
- Python中encode/decode函数中的errors入参
- 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)
- form表单的两种提交方式,submit和button的用法
- json解析的四种方法