UVA 11488(Hyper Prefix Sets-Trie统计)

Pre x goodness of a set string is length of longest common pre x*number of strings in the set. For

example the pre x goodness of the set f000,001,0011g is 6.You are given a set of binary strings. Find

the maximum pre x 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 pre x goodness among all possible subsets of n binary strings.

Sample Input

4

4

0000

0001

10101

010

2

01010010101010101010

11010010101010101010

3

010101010101000010001010

010101010101000010001000

010101010101000010001010

5

01010101010100001010010010100101

01010101010100001010011010101010

00001010101010110101

0001010101011010101

00010101010101001

Sample Output

6

20

66

44

扔到一个Trie上,分别统计每个节点的答案

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<queue>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXNode (MAXN*MAXLen)
#define Sigma_size (2)
#define MAXN (50000+10)
#define MAXLen (200+10)
#define MAXT (20+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
class Trie
{
public:
	int ch[MAXNode][Sigma_size];
	int v[MAXNode],siz;
	Trie(int _siz=0):siz(_siz){ans=0; MEM(ch) MEM(v)}
	void mem(int _siz=0){siz=_siz; ans=0; MEM(ch) MEM(v)	}
	int idx(char c){return c-'0';}
	void insert(char *s,int val=0)
	{
		int u=0,n=strlen(s);
		Rep(i,n)
		{
			int c=idx(s[i]);
			if (!ch[u][c])
			{
				++siz;
				MEM(ch[siz]);
				ch[u][c]=siz;
			}
			u=ch[u][c];
		}
		v[u]+=val;
	}
	void find(char *s)
	{
		int u=0,n=strlen(s);
		Rep(i,n)
		{
			int c=idx(s[i]);
			if (!ch[u][c])
			{
				return;
			}
			u=ch[u][c];
		}
	}
	ll ans;
	void calc(int u,ll l)
	{
		Rep(c,Sigma_size)
		{
			if (!ch[u][c]) continue;
			calc(ch[u][c],l+1);
			v[u]+=v[ch[u][c]];
		}
		ans=max(ans,v[u]*l);
	}

}S;
int T,n;
char s[MAXLen];
int main()
{
//	freopen("uva11488.in","r",stdin);

	cin>>T;
	while(scanf("%d",&n)==1)
	{
		S.mem();
		For(i,n)
		{
			scanf("%s",s);
			S.insert(s,1);

		}
		S.calc(0,0);
		cout<<S.ans<<endl;
	}
	return 0;
}
时间: 2024-10-11 07:38:22

UVA 11488(Hyper Prefix Sets-Trie统计)的相关文章

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 goodnes

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 goodnes

UVa 11488 Hyper Prefix Sets

方法:Trie 本题其实就是trie的实现,每个节点需要记录两个值,深度 和 visit的次数,答案便是 max(深度 * visit的次数). 数组实现code: #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <string> #include <vector> #include <stack>

UVA 11488 Hyper Prefix Sets 字典树

模板题,字典树最基本的操作 在看别人的板子的时候学到了一点小技巧 下面贴AC代码,顺便补一补字典树相关,顺便放一下橙子讲课的笔记 Trie三兄弟--标准Trie.压缩Trie.后缀Trie 字符串模式匹配算法--BM.Horspool.Sunday.KMP.KR.AC算法一网打尽 #include<bits/stdc++.h> using namespace std; const int MAX = 5e4 + 5; string s; int n, t, ans; struct Trie {

HDU 11488 Hyper Prefix Sets (字符串-Trie树)

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 goodnes

Hyper Prefix Sets

uva11488:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2483 题意:给你n个串,对于一个前缀,如果出现k次,就会得到前缀的长度*k,现在让你求长度*k的最大值. 题解:用trie树来搞.把每个串插入到trie中,记录每个子串出现的次数以及长度,trie树很容易实现,然后插完之后,把每个节点遍历一遍

UVa11488-Hyper Prefix Sets(trie树)

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 goodnes

UVA 11488-Hyper Prefix Sets(Trie)

题意: 给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值 分析: val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树求出最大值 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cst

UVa 11488 超级前缀集合(Trie的应用)

https://vjudge.net/problem/UVA-11488 题意: 给定一个字符串集合S,定义P(s)为所有字符串的公共前缀长度与S中字符串个数的乘积.比如P( {000, 001, 0011} ) = 6.给n个01串,从中选择一个集合S,使得P(S)最大. 思路: 建立字典树,边插入边统计答案即可. 用两个变量分别记录前缀数量和前缀长度,每次插入时动态更新两者乘积. 1 #include<iostream> 2 #include<cstdio> 3 #includ