POJ 2001-Shortest Prefixes(Trie 入门)
题意:给你一组串,求每个串在组中唯一标志的最短前缀
分析:保存树上经过各节点的单词个数,扫描每个串当经过节点单词个数是一(能唯一标志)结束
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 20010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct Trie{
int ch[N][];
int val[N],num;
void init(){
num=;
memset(ch[],,sizeof(ch[]));
}
int idx(char c){return c-'a';}
//建树
void build(char *s){
int u=,n=strlen(s);
for(int i=;i<n;++i){
int v=idx(s[i]);
if(!ch[u][v]){
memset(ch[num],,sizeof(ch[num]));
ch[u][v]=num++;
}
u=ch[u][v];
val[u]++;
}
}
//查询
void query(char *s){
int u=,n=strlen(s);
for(int i=;i<n;++i){
int v=idx(s[i]);
u=ch[u][v];
printf("%c",s[i]);
if(val[u]==){
break;
}
}
printf("\n");
}
}trie;
int main()
{
trie.init();
char a[][];
int id=;
while(~scanf("%s",a[id])){
trie.build(a[id]);
id++;
}
for(int i=;i<id;++i){
printf("%s ",a[i]);
trie.query(a[i]);
}
return ;
}
POJ 2001-Shortest Prefixes(Trie 入门)的更多相关文章
- poj 2001 Shortest Prefixes trie入门
Shortest Prefixes 题意:输入不超过1000个字符串,每个字符串为小写字母,长度不超过20:之后输出每个字符串可以简写的最短前缀串: Sample Input carbohydrate ...
- 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 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 ...
随机推荐
- ionicPopup确认对话框
$ionicPopup.confirm({ title: $rootScope.app_name, template: 'Do you want to add this to database?', ...
- SQL的表连接
每天给自己扫盲,让自己变得越博学. 继续学习<程序员的SQL金典>,这回我们来看看表连接相关的内容.表连接的相关知识在实际的项目开发当中,使用非常广. 所谓表连接,就是通过关联多张表,从而 ...
- DIV中TABLE居的2种方式
<html><head><title>测试页面</title></head><body><div width=" ...
- [转载]js 遍历数组对象
有一个JSON数组如下 all = {"error":0,"content":[{"name":"北京","v ...
- spoj 1436
用并查集看一下是否会围成一个环 若围成环直接输出NO 当然 当 m >= n 时必然会围成环直接输出NO #include <algorithm> #include < ...
- POJ2527+多项式除法
模拟一遍即可. 注意一些特殊情况,见代码. #include<stdio.h> #include<stdlib.h> #include<math.h> #inclu ...
- json中jobject
Json.net codeplex :http://www.codeplex.com/Json 原本感觉Newtonsoft.Json和.net自己的JavaScriptSerializer相差无几, ...
- [java线段树]2015上海邀请赛 D Doom
题意:n个数 m个询问 每个询问[l, r]的和, 再把[l, r]之间所有的数变为平方(模为9223372034707292160LL) 很明显的线段树 看到这个模(LLONG_MAX为922337 ...
- [矩阵快速幂]HDOJ4565 So Easy!
题意:给a, b, n, m 求 $\left \lceil ( a+ \sqrt b )^n \right \rceil$ % m 看到 $( a+ \sqrt b )^n$ 虽然很好联想到共轭 但 ...
- 关于DataTables一些小结
最近项目中使用了DataTables,故小结了一下. 导入CSS文件<link rel="stylesheet" href="<%=base %>/js ...