题目:
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;
}