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 ...
随机推荐
- mybatis generator 覆盖xml文件
mybatis generator默认采用追加方式生成,所以我们如果要重新生成代码的时候那么要先删除原来的文件. 解决办法: 1:创建一个自定义补丁类. OverwriteXmlPlugin.java ...
- SCOI2013 密码
题目描述: Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进. 通过翻阅古籍,Fish 得知了这个密码的相关信息: 该密 ...
- Python 字符编码-文件处理
.read #读取所有内容,光标移动到文件末尾.readable #判断文件是否可读.readline #读取一行内容,光标移动到第二行首部.readlines #读取每一行内容,存放于列表中.wri ...
- tomcat排错以及优化
jstack $PID #查看java进程的状态,分析tomcat卡死原因,定位java进程卡死的函数,调整代码 #RUNNABLE,在虚拟机内执行的.运行中状态,可能里面还能看到locked字样,表 ...
- jQuery对table排序
<script> //col对应列,cmp两数比较方法,返回值为TRUE,FALSE function sort(col, cmp) { var table = $("#test ...
- Python之文件处理-递归删除特定文件
Python之文件处理-递归删除特定文件 #!/usr/bin/env python # -*- coding:utf-8 -*- import os def delete_particular_fi ...
- SQLAlchmy模块详解
之前写过一篇博客介绍过sqlalchemy的基本用法,本篇博客主要介绍除增删改查以外SQLAlchemy对数据库表的操作,主要内容有单表操作.一对多操作.多对多操作. 一.单表操作 单表操作的增删改查 ...
- ppt_旋转抽奖_制作步骤
1 ppt制作抽奖转盘 插入饼状图,更改比例,更改颜色(一般选取相邻的一组颜色匹配比较好看): 插入竖文本框,编辑每一个部分为特等奖.一等奖.二等奖.三等奖: 全部选中,ctrl+G组合: 添加陀螺旋 ...
- IDEA 2018集成MyBatis Generator 插件 详解
1.修改maven的pom文件 只需要将如下依赖添加到pom.xml文件中即可.(注意此处是以plugin的方式,放在<plugins> </plugins>中间即可) < ...
- python基础示例
7.写代码 (1)实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! _name = "seven" _pwd = " ...