Shortest Prefixes
poj2001:http://poj.org/problem?id=2001
题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的。
题解:直接用trie树来搞。每个节点维护一个val值,每次插入经过该点值,把该点val加1,然后查询的时候,如果当前节点val是1,则说明只有一个串经过这里,此时就可以停止输出,返回即可。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define maxn 1100000
using namespace std;
struct Nod { //0为无效值
int lnk[], val;
void init() {
memset(lnk, , sizeof(lnk));
val = ;
}
};
const char BASE = 'a';
struct Trie {
Nod buf[maxn];
int len;
void init() {
buf[len=].init();
}
void insert(char * str) {
int now = ;
for(int i = ; str[i]; i ++) {
int & nxt = buf[now].lnk[str[i]-BASE];
if(!nxt)buf[nxt=++len].init();
now = nxt;
buf[now].val++;
}
}
void search(char * str) {
int now = ;
for(int i = ; str[i]; i ++) {
int & nxt = buf[now].lnk[str[i]-BASE];
printf("%c",str[i]);
if(buf[nxt].val==){
printf("\n");
return;
}
//if(!nxt) return 0;
now = nxt;
}
printf("\n");
}
} trie;
char str[][];
int main(){
int top=;
trie.init();
while(~scanf("%s",str[++top])){
trie.insert(str[top]);
}
for(int i=;i<=top;i++){
printf("%s ",str[i]);
trie.search(str[i]);
}
}
Shortest Prefixes的更多相关文章
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- POJ 2001:Shortest Prefixes
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16782 Accepted: 728 ...
- 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 trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- Shortest Prefixes(trie树唯一标识)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15948 Accepted: 688 ...
- POJ 2001 Shortest Prefixes(字典树活用)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21651 Accepted: 927 ...
- poj2001 Shortest Prefixes(字典树)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21642 Accepted: 926 ...
- POJ 2001 Shortest Prefixes 【 trie树(别名字典树)】
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15574 Accepted: 671 ...
- 【POJ2001】Shortest Prefixes
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18724 Accepted: 810 ...
- poj 2001 Shortest Prefixes(字典树trie 动态分配内存)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15610 Accepted: 673 ...
随机推荐
- zookeeper选举代码分析
本文将以zookeeper的3.4.6版本作为源码分析版本.主要的代码类包括QuorumPeerMain.QuorumPeer.FastLeaderElection.QuorumMaj等. 假设有a, ...
- MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- cocos2dx内存管理的一些看法
今年年初进入一家游戏公司,正式开始游戏引擎的学习,之前的ios学习,对现在的游戏引擎学习还是有很大的帮助的,虽然使用c++,但却能时刻感受到ios框架对于cocos2dx的巨大影响. 由于之前一直使用 ...
- android编程中setLayoutParams方法设置
第一篇 private LinearLayout generateHeadOfControl() { LinearLayout LayoutHead = createLayout(LinearLayo ...
- Android 图片选择器
图片选择器,遍历系统所有图片并显示,点击查看大图,长按选中,并将结果返回 字体颜色res/color建立text_selecor.xml <selector xmlns:android=&quo ...
- android string.xml里的空格字符
在string.xml定义字符串的时候常常要用到空格, 直接用键盘敲的话不知道是几个空格,常常看错了,导致误删. 假设用 来替代空格的话,就好非常多. 另外使用%1$s,%1$d能够在一个字符串里定义 ...
- android:Adb connection Error:远程主机强迫关闭了一个现有的连接
用真机调试程序的时候,eclipse的console总是出现如下的错误“Adb connection Error:远程主机强迫关闭了一个现有的连接” 问题出现的原因:这是ddms调用adb引发的. 经 ...
- 实现ARC文件与MRC文件互转,和混合使用。
这段时间做项目是以MRC为主的 但是某些第三方现在都只能支持ARC了 找到了这篇文章 可谓是帮了大忙 亲测完全可用喔:-O 如何在未使用arc的工程中引入一个使用了arc特性的文件,如何在arc工程中 ...
- [转] C语言多维数组与多级指针
http://c.biancheng.net/cpp/html/477.html 多维数组与多级指针也是初学者感觉迷糊的一个地方.超过二维的数组和超过二级的指针其实并不多用.如果能弄明白二维数组与二级 ...
- codevs2059逃出克隆岛(传送门bfs)
/* 和普通的迷宫问题类似只是多了一个叫传送门的东西 对于传送门的处理: 每当跑到传送门就把其余所有传送门周围的点都入队 传送门之间不花费时间并且从不是传送门的点走到传送门 也不花费时间花费时间的(好 ...