POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001
Description
A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string 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
The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.
Output
The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.
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
Source
Solution
题意
给出若干个由小写字母组成的单词,对每个单词找出最短的前缀,使得该前缀不是其他任何字符串的前缀。
如果整个单词都是其他单词的前缀就输出整个单词。
题解
Trie
标记每个结点结束的前缀是多少个单词的前缀,查找时只要找到某个唯一的前缀就输出。
Code
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 1e4 + 10;
int nxt[maxn][30];
int val[maxn];
int tot = 1;
int index(char c) {
return c - 'a';
}
void insert(string s) {
int len = s.size();
int p = 0;
for (int i = 0; i < len; ++i) {
int c = index(s[i]);
if (!nxt[p][c]) {
nxt[p][c] = tot++;
}
p = nxt[p][c];
++val[p];
}
}
void query(string s) {
int len = s.size();
int p = 0;
for (int i = 0; i < len; ++i) {
int c = index(s[i]);
p = nxt[p][c];
cout << s[i];
if(val[p] == 1) return;
}
}
string s[maxn];
int n = 1;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
while(cin >> s[n]) {
insert(s[n]);
n++;
}
for(int i = 1; i < n; ++i) {
cout << s[i] << " ";
query(s[i]);
cout << endl;
}
return 0;
}
POJ 2001 Shortest Prefixes (Trie)的更多相关文章
- 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: 15574 Accepted: 671 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
- 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(字典树,经典题,求最短唯一前缀)
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 ...
- POJ 2001 Shortest Prefixes 【Trie树】
<题目链接> 题目大意: 找出能唯一标示一个字符串的最短前缀,如果找不出,就输出该字符串. 解题分析: Trie树的简单应用,对于每个单词的插入,都在相应字符对应的节点 num 值+1 , ...
- poj 2001 Shortest Prefixes(特里)
主题链接:http://poj.org/problem?id=2001 Description A prefix of a string is a substring starting at the ...
随机推荐
- [题解]Print a 1337-string...-数学(codeforces 1202D)
题目链接:https://codeforces.com/problemset/problem/1202/D 题意: 构造一串只由 ‘1’,‘3’,‘7’ 组成的字符串,使其 ‘1337’ 子序列数量为 ...
- Python 与 C 对比
到目前为止,我接触最多两种语言应该就是python 和 C 语言了. 个人理解 1. 执行速度不同, python为解释性语言,C是编译型语言(需要编译器) 2. python 是基于C的实现,C中很 ...
- java--二叉树解析及基本实现
一.二叉树的结构 在进行链表结构开发的过程之中,会发现所有的数据按照首尾相连的状态进行保存,那么 在进行数据查询时为了判断数据是否存在,这种情况下它所面对的时间复杂度就是"O(n)" ...
- POJ 3020:Antenna Placement(无向二分图的最小路径覆盖)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6334 Accepted: 3125 ...
- Python之str型转成int型
str转int: def fn(x,y): return x*10+y def char2num(s): ':9}[s] # 特别注意这里,后面还有个 [s] ')))) '))) 输出如下: < ...
- Docker 清理容器 log 日志
原文 Docker 清理容器 log 日志 docker logs <容器ID> 是常用命令,来查看容器运行日志,但时间长了之后,就会发现越来越慢,log 太多了,这时就需要清理一下. 先 ...
- hive数据库导入与导出
原文连接:https://www.cnblogs.com/654wangzai321/p/9970321.html 把Hive表数据导入到本地 table->local file insert ...
- 以python为例讲解闭包机制
以python为例讲解闭包机制 缘起 在学习JS的过程中,总是无可避免的接触到闭包机制,尤其是接触到react后,其函数式的编程思想更是将闭包发扬光大,作为函数式编程的重要语法结构,python自然也 ...
- 编译lineageos1
lineageos 前奏 -- 搭建编译环境 我目前使用的手机是红米note4x 目前lineageos15.1已经官方支持,下文是按照官网文档编译安装包操作总结 构建环境搭建主要参考官方文档 参考文 ...
- Goldengate 部署oracle10g在 rac asm环境,完整教程
前言 Goldengate再rac 环境部署,和单机部署区别还是有点大,主要存在环境上. 环境 oracle10g ,sid=rac 准备工作 1.在rac节点,配置监听动态注册,确保goldenga ...