UV 11488 Hyper Prefix Sets (Trie), 11488 trie

Source: Internet
Author: User

UV 11488 Hyper Prefix Sets (Trie), 11488 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,001 1} 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


Question:
N strings must be selected to maximize the product of the length of the Public prefix of the selected string and the number of the selected strings.
Analysis:
Simple and crude Trie template question.
Add two information for each node in Tire: the depth of the node and the number of visits to the node cup. Finally, find the maximum value of the two information, and add strings while maintaining the information.


/* * * 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 5000007using 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;}





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.