UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483
Hyper Prefix Sets
Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodness of theset {000,001,0011} is 6.You are given a set of binarystrings. Find the maximum prefixgoodness among
all possible subsets of these binary strings.
Input
First line of the input contains T(≤20)the number of test cases. Each of the test cases start withn(≤50000) the number of strings. Eachof the next n lines contains a string containing only 0 andMaximum length of each of thesestring
is 200.
Output
For each test case output the maximumprefix goodness among all possible subsets of n binarystrings.
Sample Input
4
4
0000
0001
10101
010
2
01010010101010101010
11010010101010101010
3
010101010101000010001010
010101010101000010001000
010101010101000010001010
5
01010101010100001010010010100101
01010101010100001010011010101010
00001010101010110101
0001010101011010101
00010101010101001
Output for Sample Input
6
20
66
44
Problem Setter : Abdullah Al Mahmud
Special Thanks : Manzurur Rahman Khan
题意:
给出N个字符串,要求选出若干个,使得选中的字符串的公共前缀长度与选中字符串的个数的乘积最大。
分析:
简单粗暴的Trie模板题。
对于Tire中的每个结点加入两个信息:该结点的深度及该结点杯訪问的次数。最后求出这两个信息的最大值即可了,边加入字符串边维护即可。
/*
*
* Author : fcbruce
*
* Time : Sat 04 Oct 2014 09:17:50 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm 2
#define maxn 5000007 using namespace std; struct Trie
{
int ch[maxn][maxm];
int deep[maxn];
int cnt[maxn][maxm];
int MAX;
int sz; Trie()
{
sz=1;
deep[0]=0;
MAX=0;
memset(cnt[0],0,sizeof cnt[0]);
memset(ch[0],0,sizeof ch[0]);
} void clear()
{
sz=1;
deep[0]=0;
MAX=0;
memset(cnt[0],0,sizeof cnt[0]);
memset(ch[0],0,sizeof ch[0]);
} int idx(const char ch)
{
return ch-'0';
} void insert(const char *s)
{
for (int i=0,j=0;s[i]!='\0';i++)
{
int c=idx(s[i]);
if (ch[j][c]==0)
{
memset(ch[sz],0,sizeof ch[sz]);
memset(cnt[sz],0,sizeof cnt[sz]);
deep[sz]=i+1;
ch[j][c]=sz++;
}
j=ch[j][c];
cnt[j][c]++;
MAX=max(MAX,deep[j]*cnt[j][c]);
}
}
}trie; char str[233]; int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int T_T;
scanf("%d",&T_T); while (T_T--)
{
trie.clear(); int n;
scanf("%d",&n); for (int i=0;i<n;i++)
{
scanf("%s",str);
trie.insert(str);
} printf("%d\n",trie.MAX);
} return 0;
}
UVA 11488 Hyper Prefix Sets (Trie)的更多相关文章
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- UVA - 11488 Hyper Prefix Sets(trie树)
1.给n个只含0.1的串,求出这些串中前缀的最大和. 例1: 0000 0001 10101 010 结果:6(第1.2串共有000,3+3=6) 例2: 01010010101010101010 1 ...
- uva 11488 Hyper Prefix Sets(狂水)
题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11488 Hyper Prefix Sets (字典树)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVa 11488 - Hyper Prefix Sets
找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...
- HDU 11488 Hyper Prefix Sets (字符串-Trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- Hyper Prefix Sets
uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&am ...
- UVa11488-Hyper Prefix Sets(trie树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
随机推荐
- MyEclipse2017修改Web Context Root
1,复制一个已经存在的项目,并修改项目名 2,选中项目右键选择properities,打开. 但是这里的web context root无法修改 3,删除web显示properties的所有属性,输入 ...
- python 删除/查找重复项
l = [1,2,3,2,1] # l = ['你','我','他','她','你'] for i in l: print("the %s has found %s" % (i, ...
- live555简介
live555 编辑 目录 1live555简介 2Live555 Streaming Media整体框架 3openRTSP客户端流程 1live555简介编辑 Live555 是一个为 ...
- delphi GDI+ [1]
摘抄自:万一的博客 安装头文件:http://www.cnblogs.com/del/archive/2008/06/06/1215319.html 目录 基本使用方法(绘制直线) 绘制一组直线 绘制 ...
- db2数据库,表相乘,直接扩大表数据
T1 表 SEQ表 想得到结果集为: 语句: SELECT * FROM (SELECT * FROM seq,t1) u LEFT JOIN t1 ON u.id=t1.id AND u.jjh=t ...
- JqueryValidate 修改 为根据ID验证
<!--修改validate根据ID验证 --> <script type="text/javascript"> if ($.validator) { $. ...
- Swift - 修改导航栏“返回”按钮文字,图标
Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037 项目中常常会使用 UINavigationController 对各个页面进行导 ...
- 九度oj 题目1473:二进制数(stack)
题目1473:二进制数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:9371 解决:2631 题目描述: 大家都知道,数据在计算机里中存储是以二进制的形式存储的. 有一天,小明学了C语 ...
- python-gzip解压缩(实验吧SOS)
本题看着很简单,就是在弄出来的老是乱码,看了pcat的wp还是不行,下面的评论说可能是python版本问题,改版本太麻烦,试了一下先gzip解压,得到的文件在打开就不是乱码了,代码如下: # -*- ...
- 【BZOJ1758】重建计划(点分治)
题意: 给定一棵n个点的树,每条边有权值.求一条链,这条链包含的边数在L和U之间,且平均边权最大.N﹤=100000 思路:RYZ作业 二分答案再点分治,寻找是否有大于0且边数在L和U之间的链 f[i ...