1174: [Balkan2007]Toponyms

题目链接

题目大意:有一个字符集合,从其中找一些字符串出来,最大化这些字符串的LCP长度*字符串个数

题解:稍有常识的人就能看出,这是一道Trie树裸题,在插入的时候顺手统计答案就可以了

然而这题卡空间,需要用邻接表存储

我的收获:……

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>
using namespace std;

int read() {
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

#define maxn 5000010
#define LL long long

int n, rt, ToT, m, val[maxn], head[maxn], nxt[maxn], to[maxn];
char ec[maxn];

LL ans;
void insert() {
    int u = rt;
    val[u]++;
    char C = getchar();
    for(int i = 0; C != '\n'; i++, C = getchar()) {
        int v = -1;
        for(int e = head[u]; e; e = nxt[e])
            if(ec[e] == C){ v = to[e]; break; }
        if(v < 0) to[++m] = ++ToT, ec[m] = C, nxt[m] = head[u], head[u] = m, v = ToT;
        val[u = v]++;
        ans = max(ans, (LL)val[u] * (i + 1));
    }
    return ;
}

int main() {
    n = read(); rt = ToT = 1;
    for(int i = 1; i <= n; i++) insert();

    printf("%lld\n", ans);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值