字典树-Hyper Prefix Setstimu:

题目:

Prefix goodness of a set string is length of longest common prefix*number of strings in the set. Forexample the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Findthe 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 string

4

4

0000

0001

1010

1010

Sample Output

6

题目大意:

给你多个01的字符串,找到他们的公共前缀长度s,和有该公共前缀的字符串个数sum,求s*sum的最大值。

思路:

这是字典树的模板题,边插入边计算当前的最大值。

https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html

#include <bits/stdc++.h>
using namespace std;
#define maxn  100000+5
int tree[maxn][2];  //树
int sum[maxn];   //第k个前缀出现次数
int tot;
int ans;
void insert(string s)
{
    int len=s.length();
    int root=0;
    for(int i=0;i<len;i++)
    {
        int id=s[i]-'0';     //每个节点的子节点,0或者1位置
        if(tree[root][id]==0)   //该位置没有出现该字母
            tree[root][id]=++tot;//给子节点编号
            root=tree[root][id];//更新当前根节点
           sum[root]++;  //前缀出现次数
          if((i+1)*sum[root]>ans)
                ans=(i+1)*sum[root];  //前缀长度*出现次数

    }

}
int main()
{
    int T,n;
     string ssr;
   cin>>T;
    while(T--)
    {
        ans=0;tot=0;
        memset(sum,0,sizeof(sum));
        memset(tree,0,sizeof(tree));
        cin>>n;
       while(n--)
       {
         cin>>ssr;
          insert(ssr);
       }
        cout<<ans<<endl;

    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值