Shortest Prefixes(poj 2001)
题意:给出n个单词(1<=n<=1000),求出每个单词的非公共前缀,如果没有,则输出自己。
/*
字典树
在裸字典树的基础上,设置一个sum数组,sum[i]表示i这个节点被用过几次,当我们扫一个单词时,
如果它的某一个字母只用过一次,即只有它用过,那么这以前就是它的前缀。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 21
#define M 1010
using namespace std;
int trie[M*N][],sum[M*N],n,tot=;
char s[M][N];
void build(int num,int rt)
{
int len=strlen(s[num]);
for(int i=;i<len;i++)
{
int x=(int)s[num][i]-''+;
if(!trie[rt][x])trie[rt][x]=++tot;
rt=trie[rt][x];
sum[rt]++;
}
}
void query(int num,int rt)
{
int len=strlen(s[num]);
for(int i=;i<len;i++)
{
int x=(int)s[num][i]-''+;
rt=trie[rt][x];
cout<<s[num][i];
if(sum[rt]==)break;
}
}
int main()
{
while(scanf("%s",s[++n])!=EOF)
{
build(n,);
}
n--;
for(int i=;i<=n;i++)
{
printf("%s ",s[i]);
query(i,);
printf("\n");
}
return ;
}
Shortest Prefixes(poj 2001)的更多相关文章
- POJ 2001 Shortest Prefixes(字典树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- POJ2001 Shortest Prefixes (Trie树)
直接用Trie树即可. 每个节点统计经过该点的单词数,遍历时当经过的单词数为1时即为合法的前缀. type arr=record next:array['a'..'z'] of longint; w: ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- pat 1046 Shortest Distance(20 分) (线段树)
1046 Shortest Distance(20 分) The task is really simple: given N exits on a highway which forms a sim ...
- 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: 21651 Accepted: 927 ...
- Shortest Prefixes(trie树唯一标识)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15948 Accepted: 688 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
随机推荐
- Proteus中的 PIC10/12/16 MCUs编译器无法下载的问题
当你打开网站时,点击该软件下载会发现如下页面: google一下会出现这个界面,大意是这个版本的编译器太老了,已经被某些更加高级的编译器给取代了(qaq心痛) 然后我就开始FQ到处google,Sou ...
- ACM_走楼梯Ⅱ
走楼梯Ⅱ Time Limit: 2000/1000ms (Java/Others) Problem Description: 有一楼梯共N+1级,刚开始时你在第一级,若每次能走M级(1<=M& ...
- 236 Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先
给定一棵二叉树, 找到该树中两个指定节点的最近公共祖先. 详见:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tre ...
- C#知识点-枚举器和迭代器
一.几个基本概念的理解 问题一:为什么数组可以使用foreach输出各元素 答:数组是可枚举类型,它实现了一个枚举器(enumerator)对象:枚举器知道各元素的次序并跟踪它们的位置,然后返回请求的 ...
- TypeError: string indices must be integers, not str
1. TypeError: string indices must be integers, not str 字符串类型取第index个字符的时候,应该传入int而不是str.如 1 a='abcde ...
- cocos2dx使用lua和protobuf
为了使游戏开发更加方便快捷,我继续了protobuf在lua下的尝试. socket使用的是cocos2dx集成的websocket. 先说下环境:cocos2d-x-2.2.1 + protobuf ...
- 在struct 中使用string,赋值会报错
struct中最好使用char来代替string,因为string的大小不是固定的
- 业余开发Android App的架构演变
闲暇之余,开发了一款休闲类app,虽然用户量不多,但确实花了不少心血在这上面.然而,开发出来的结果,与之前想好的架构,还是有不少区别. 下面,记录下这款app架构的演变: 最初,只想写个app,能与机 ...
- 华硕(ASUS)X554LP笔记本一开机就进入aptio setup utility 问题的解决
某次因大意一直未插电,华硕(ASUS)X554LP笔记本后来没电关机.后来每次一开机就进入aptio setup utility界面,按F9调入默认配置,F10保存后退出,重启仍然进入aptio se ...
- js删除局部变量
alert('value:'+str+'\ttype:'+typeof(str)) //声明变量前,引用 var str="dd"; alert('value:'+str+'\tt ...