POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 15574 | Accepted: 6719 |
Description
is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that
uniquely identifies the word it represents.
In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list
that begins with "car".
Input
Output
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
看完题后,感慨:简直不能忍,这么裸的字典树。接下来就是无脑式敲 Trie 树了。这个题目能够拿来练练手速~。另外。一时没到比較合适的函数名,望 勿喷
题解就略了,容我贴下代码~
/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define rep(i,a,b) for(int i = a;i <= b;i++)
#define rep1(i,a) for(int i = 1;i <= a;i++)
#define rep0(i,a) for(int i = 0;i < a;i++)
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)
#define fst first
#define snd second
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x) cout<<x<<""
/****************************>>>>SEPARATOR<<<<****************************/
const int maxk = 26;
const int maxl = 20+5;
int N,M;
struct Node
{
int cnt;
Node* pNext[maxk];
Node() : cnt(0)
{
rep0(i,maxk) pNext[i] = NULL;
}
};
struct Trie
{
Node* const pRoot;
Trie() : pRoot(new Node()) {}
void AddWord(const char str[],int len);
// int FindPredix(const char str[],int len);
void Query(const char str[],int len);
void Release(const Node *p);
}dic;
void Trie::AddWord(const char str[],int len)
{
Node* ptr = pRoot;
for(int i = 0;i < len;i++)
{
int nPos = str[i] - 'a';
if(ptr->pNext[nPos] == NULL) ptr->pNext[nPos] = new Node();
ptr->cnt++;
ptr = ptr->pNext[nPos];
}
ptr->cnt++;
}
void Trie::Release(const Node* p)
{
for(int i = 0;i < maxk;i++) if(p->pNext[i] != NULL) Release(p->pNext[i]);
delete p;
}
void Trie::Query(const char str[],int len)
{
Node* ptr = pRoot;
for(int i = 0;i < len;i++)
{
int nPos = str[i] - 'a';
if(ptr->pNext[nPos] == NULL) return;
if(ptr->cnt == 1) return;
putchar(str[i]);
ptr = ptr->pNext[nPos];
}
}
char buf[1005][maxl];
int main()
{
//FIN;
int cnt = 0;
while(~scanf("%s",buf[cnt]))
dic.AddWord(buf[cnt],strlen(buf[cnt])),cnt++;
for(int i = 0;i < cnt;i++)
{
printf("%s ",buf[i]);
dic.Query(buf[i],strlen(buf[i]));
puts("");
}
dic.Release(dic.pRoot);
return 0;
}
POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】的更多相关文章
- 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(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- 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 ...
- poj 2001 Shortest Prefixes
字典树的简单应用. #include<stdio.h> #include<string.h> ][]; struct node{ int cnt; node *next[]; ...
- poj 2001 Shortest Prefixes(字典树)
题目链接:http://poj.org/problem?id=2001 思路分析: 在Trie结点中添加数据域childNum,表示以该字符串为前缀的字符数目: 在创建结点时,路径上的所有除叶子节点以 ...
随机推荐
- POJ 3249:Test for Job(拓扑排序+DP)
题意就是给一个有向无环图,每个点都有一个权值,求从入度为0的点到出度为0点路径上经过点(包括起点终点)的权值和的最大值. 分析: 注意3点 1.本题有多组数据 2.可能有点的权值是负数,也就是结果可能 ...
- 编译静态库tinyxml2
tinyxml的makefile文件默认是编译可执行的二进制文件xmltest. 需要改成静态库. 更改OUTPUT := xmltest 为:OUTPUT := libtinyxml.a 删除SR ...
- POJ 3678 Katu Puzzle(2-SAT,合取范式大集合)
Katu Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9987 Accepted: 3741 Descr ...
- CTSC2018 旅游记
我即使是死了,尸体烂在棺材里,也要用这腐朽的声音喊出: LJCCF!!!! DAY -3 体育中考AK了! 顿时感觉中考稳了(虽然竞赛已经特招) 新目标:我要用三种方式考上SZMS! DAY -1 成 ...
- Java面试题之如何防止重复下单问题?
在电商环境下,如何防止重复下单这种问题,很常见,并且解决方案有很多种,我经过百度,并且加入我的理解唠嗑几句: 流程: ①当进入商品详情页时,去生成一个全局唯一ID(可用雪花算法): ②将这个全局唯一I ...
- mybatis 从数据库查询的信息不完整解决办法
List<Product> products = productService.getProductListWithPage(productQuery); 今天碰到一个很奇怪的现象,上面的 ...
- set与map的区别
STL中:MAP的节点是一对数据. SET的节点是一个数据. Map使用关键值Key来唯一标识每一个成员 map可以重复.set是集合 都属于关联容器 只不过, map的形式 map& ...
- FusionCharts参数大全
原文发布时间为:2010-01-11 -- 来源于本人的百度文章 [由搬家工具导入] Fusioncharts 参数 objects ANCHORS 锚点 用于标识line或area的数值点 支持效果 ...
- Some lines about EF Code First migration.
Some lines about EF Code First migration: 一. 模型设计 1. 遵循EF标准,注意表关系配对 2. 数据模型里尽量把必须的属性和说明都写全 3. EF默认id ...
- JSP-Servlet-SpringMVC
作者:码思客链接:https://zhuanlan.zhihu.com/p/37612412来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本篇文章,我们来讲讲技术,系 ...