码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11488(Hyper Prefix Sets-Trie统计)

时间:2015-04-06 01:07:22      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

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;
}




UVA 11488(Hyper Prefix Sets-Trie统计)

标签:

原文地址:http://blog.csdn.net/nike0good/article/details/44893569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!