UVA 11488 (Hyper Prefix sets-trie statistics)

Source: Internet
Author: User

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 is 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 () 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 the 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


Throw on a trie and count the answers to each node separately


#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 statistics)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.