题意:
给一个01串的集合,一个集合的幸运值是串的个数*集合中串的最大公共前缀 ,求所有子集中最大幸运值
分析:
val[N]表示经过每个节点串的个数求幸运值 求就是每个节点值*该节点的深度 搜一遍树求出最大值
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int>P; typedef long long ll; #define lson l,m,rt<<1 #define pi acos(-1.0) #define rson m+1,r,rt<<11 #define All 1,N,1 #define N 10000010 #define read freopen("in.txt", "r", stdin) const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int mod = 1000000007; struct Trie{ int ch[N][3]; int val[N],num; void init(){ num=1; memset(ch[0],0,sizeof(ch[0])); memset(val,0,sizeof(val)); } //建树 void build(char *s){ int u=0,len=strlen(s); for(int i=0;i<len;++i){ int v=s[i]-‘0‘; if(!ch[u][v]){ memset(ch[num],0,sizeof(ch[num])); ch[u][v]=num++; } u=ch[u][v]; val[u]++; } } //bfs ll query(){ queue<P>q; ll maxv=0; q.push(P(0,0)); while(!q.empty()){ P p=q.front(); q.pop(); int u=p.first; int h=p.second; maxv=max(maxv,1LL*val[u]*h); for(int i=0;i<2;++i){ if(ch[u][i]){ q.push(P(ch[u][i],h+1)); } } } return maxv; } }trie; int main() { int t,n; char str[310]; scanf("%d",&t); for(int o=0;o<t;++o){ trie.init(); scanf("%d",&n); for(int i=0;i<n;++i){ scanf("%s",str); trie.build(str); } printf("%lld\n",trie.query()); } return 0; }
时间: 2024-10-07 04:28:31