uva 11488 - Hyper Prefix Sets(字典树)
H |
Hyper Prefix Sets |
Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodness 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 with n(≤50000) the number of strings. Each of the next n lines contains a string containing only 0 and 1. Maximum length of each of these string is 200.
Output
For each test case output the maximum prefix goodness among all possible subsets of n binary strings.
Sample Input Output for Sample Input
4 4 0000 0001 10101 010 2 01010010101010101010 11010010101010101010 3 010101010101000010001010 010101010101000010001000 010101010101000010001010 5 01010101010100001010010010100101 01010101010100001010011010101010 00001010101010110101 0001010101011010101 00010101010101001
|
6 20 66 44 |
字典树,每条边记录一下深度。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
using namespace std; struct edge{
int u , v , letter , sum , dep;
edge(int a = 0 , int b = 0 , int c = 0 , int d = 0 , int f = 0){
u = a , v = b , letter = c , sum = d , dep = f;
}
};
vector<edge> e;
vector<int> head , next;
int ans; void add(int u , int v , int letter , int sum , int dep){
e.push_back(edge(u , v , letter , sum , dep));
next.push_back(head[u]);
head[u] = next.size()-1;
head.push_back(-1);
} void addLibrary(string s){
int u = 0 , index = 0;
while(index < s.length()){
int Next = head[u];
while(Next != -1){
if(e[Next].letter == s[index]-'0') break;
Next = next[Next];
}
if(Next == -1) break;
e[Next].sum++;
index++;
u = e[Next].v;
}
while(index < s.length()){
add(u , head.size() , s[index]-'0' , 1 , index+1);
index++;
u = head.size()-1;
}
}
void initial(){
e.clear();
next.clear();
head.clear();
head.push_back(-1);
ans = 0;
} void readcase(){
string s;
int n;
scanf("%d" , &n);
getchar();
while(n--){
cin >> s;
//cout <<"+"<<s<<endl;
addLibrary(s);
}
for(int i = 0; i < e.size(); i++){
if(e[i].sum*e[i].dep > ans) ans = e[i].sum*e[i].dep;
}
printf("%d\n" , ans);
} int main(){
int t;
scanf("%d" , &t);
while(t--){
initial();
readcase();
}
return 0;
}
uva 11488 - Hyper Prefix Sets(字典树)的更多相关文章
- UVA 11488 Hyper Prefix Sets (字典树)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11488 Hyper Prefix Sets (Trie)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 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
找 前缀长度*符合该前缀的字符串数 的最大值 顺便练了一下字典树的模板 #include <iostream> #include <cstdio> #include <c ...
- uva 11488 Hyper Prefix Sets(狂水)
题意: 获得集合中最长前缀长度*有该前缀个数的最大值 Prefix goodness of a set string is length of longest common prefix*number ...
- 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 ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- 【leetcode】208. Implement Trie (Prefix Tree 字典树)
A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently s ...
随机推荐
- Cocos2D 添加 UIView
cocos2d是使用继承于ccnode的结点类型的层.但是我想用一个opengl来绘制,就简单的情况来说必须得加一个uiview.现转载如下: 第一部分:: 使用Cocos2D开发游戏和应用程序的时候 ...
- Struts 2 动作注释 配置动作扩展 全局开关
动作注释package com.yiibai.user.action; import org.apache.struts2.convention.annotation.Action; import o ...
- document文档碎片
var arrText = ["1","2","3","4","5","6",& ...
- NYOJ 219 An problem about date
An problem about date 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 acm的iphxer经常忘记某天是星期几,但是他记那天的具体日期,他希望你 ...
- 九度oj 题目1472:求两个多项式的和
题目描述: 输入两个多项式,计算它们的和. 每个多项式有若干对整数表示,每组整数中,第一个整数表示系数(非0),第二个整数表示该项的次数. 如由3 3 5 -2 1 4 0表示3x^5 - 2 * x ...
- nginx中access_log和nginx.conf中的log_format用法
nginx服务器日志相关指令主要有两条: 一条是log_format,用来设置日志格式; 另外一条是access_log,用来指定日志文件的存放路径.格式和缓存大小 可以参加ngx_http_log_ ...
- 【Luogu】P1972HH的项链(链表+树状数组)
题目链接 难题,所以会讲得细一些. 首先我们想如何统计区间[l,r]内不同贝壳的个数. 第一个思路就是线段树/树状数组,query(1,r)-query(1,l-1)对不对? 然而这样是不对的. 然后 ...
- 【Luogu】P1586四方定理(DP)
题目链接 此题使用DP.设f[i][j]表示数i用j个数表示,则对于所有的k<=sqrt(i),有 f[i][j]=∑f[i-k*k][j-1] 但是这样会有重复情况.所以先枚举k,再枚举i和j ...
- BZOJ 1069 [SCOI2007]最大土地面积 ——计算几何
枚举对角线,然后旋转卡壳即可. #include <map> #include <cmath> #include <queue> #include <cstd ...
- BZOJ 4503 两个串 ——FFT
[题目分析] 定义两个字符之间的距离为 (ai-bi)^2*ai*bi 如果能够匹配,从i到i+m的位置的和一定为0 但这和暴力没有什么区别. 发现把b字符串反过来就可以卷积用FFT了. 听说KMP+ ...