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 ...
随机推荐
- git使用记录八:不同提交的指定文件的差异
不同提交的指定文件的差异 git diff commit-id1 commit-id2 path-to-filename
- spring注解开发:bean的作用域与懒加载
1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...
- 如何判断索引是否生效--explain
explain 显示了MySql 如何使用索引来处理select语句以及连接表. 使用方式在select 前面加上 explain就可以了 示例:explain select id , name ta ...
- Codeforces 500B New Year Permutation( Floyd + 贪心 )
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- kubernetes容器集群部署Flannel网络
Overlay Network:覆盖网络,在基础网络上叠加的一种虚拟网络技术模式,该网络中的主机通过虚拟链路连接起来. VXLAN:将源数据包封装到UDP中,并使用基础网络的IP/MAC作为外层报文头 ...
- 前端工程师的新选择WebApp
作为新一代移动端应用分发入口,小程序的趋势明朗化,竞争也在急剧激烈化.战线从手机 QQ.QQ 浏览器.支付宝.手机淘宝,华为,小米等九家手机厂商推出“快应用”,再拉到了谷歌的 Instant App ...
- ubuntu下安装chrome谷歌浏览器
百度“chrome”然后登录谷歌浏览器官网下载deb包 cd到下载的目录下 sudo dpkg -i google-chrome*; 提示缺少依赖包,打入如下命令 sudo apt-get -f in ...
- Linux Centos 7 下部署 .NetCore + MySql + Redis + mssql2007 部署过程
1. net core 安装及运行配置 安装 1)rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-p ...
- 四、Redis通配符介绍、命令缩写介绍和后面内容介绍讲解。
1.通配符介绍 ? 匹配一个字符 * 匹配任意个(包括 0 个)字符 [] 匹配括号间任一字符,可以使用 "-" 符号表示一个范围,如 a[b-d]匹配 "ab" ...
- QT blockingmaster例子学习
dialog.h: #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include "masterthread.h&q ...