UVA - 11488 Hyper Prefix Sets

链接

https://vjudge.net/problem/UVA-11488

题解

记录字典树每个点的深度 d e p i dep_i depi和经过这个点的字符串个数 c n t i cnt_i cnti
那么答案就是 m a x { d e p i × c n t i } max\{dep_i \times cnt_i\} max{depi×cnti}

代码

#include <bits/stdc++.h>
#define maxn 10000010
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
int ans;
struct Trie
{
    int tot, trie[maxn][2], tail[maxn], cnt[maxn], dep[maxn];
    void init()
    {
        cl(trie), cl(tail), cl(cnt), cl(dep), tot=1;
    }
    void insert(int *s, int len)
    {
        int pos(1);
        for(auto i=1;i<=len;i++)
        {
            auto to = trie[pos][s[i]] ? trie[pos][s[i]] : trie[pos][s[i]]=++tot;
            dep[to] = dep[pos] + 1;
            cnt[to]++;
            pos=to;
        }
        tail[pos]++;
    }
    int calc()
    {
        int i, ans(0);
        for(i=1;i<=tot;i++)
        {
            ans=max( ans, dep[i] * cnt[i]);
        }
        return ans;
    }
}trie;
int read(int x=0)
{
    int c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-48;
    return f*x;
}
int main()
{
    int T=read(), n, i, r[300];
    char s[300];
    while(T--)
    {
        trie.init();
        n=read();
        for(i=1;i<=n;i++)
        {
            scanf("%s",s+1);
            auto len=strlen(s+1);
            for(auto i=1;i<=len;i++)r[i]=s[i]-0x30;
            trie.insert(r,len);
        }
        printf("%d\n",trie.calc());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值