Codeforces 965E 启发式合并

32 篇文章 0 订阅
32 篇文章 0 订阅

http://codeforces.com/contest/965/problem/E

E. Short Code
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arkady's code contains nn variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code.

He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names.

A string aa is a prefix of a string bb if you can delete some (possibly none) characters from the end of bb and obtain aa.

Please find this minimum possible total length of new names.

Input

The first line contains a single integer nn (1n1051≤n≤105) — the number of variables.

The next nn lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than 105105. The variable names are distinct.

Output

Print a single integer — the minimum possible total length of new variable names.

Examples
input
Copy
3
codeforces
codehorses
code
output
Copy
6
input
Copy
5
abba
abb
ab
aa
aacada
output
Copy
11
input
Copy
3
telegram
digital
resistance
output
Copy
3
Note

In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c".

In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names.

题意:给出N个字符串,保证字符串总长度<=1e5, 对于每个单词找到他的一个非空前缀代表他, 并且需要保证找的N个前缀不能有相同的,且前缀总长度最小。

题解:很容易想到字典树,那么就是在字典树上找N个节点,我们利用记忆化,并且启发式合并一个节点的子树, 然后即可。具体看代码。

#include <bits/stdc++.h>

using namespace std;

const int maxn = 100007;

int ch[maxn][26], root, tot;
bool ed[maxn];
char s[maxn];
multiset<int> c[maxn];

void dfs(int u, int d)
{
    for(int i = 0;i < 26;i ++) {
        if(ch[u][i]) {
            dfs(ch[u][i], d + 1);
            for(auto it : c[ch[u][i]]) c[u].insert(it);
        }
    }
    if(ed[u]) {
        c[u].insert(d);
    } else if(d != 0) {
        c[u].erase(prev(c[u].end()));
        c[u].insert(d);
    }
}

int main()
{
    auto add = [&] () ->void {
        int len = strlen(s);
        int now = root;
        for(int i = 0;i < len;i ++) {
            int key = s[i] - 'a';
            if(!ch[now][key]) ch[now][key] = ++tot;
            now = ch[now][key];
        }
        ed[now] = 1;
    };

    int n;
    scanf("%d", &n);
    for(int i = 1;i <= n;i ++) {
        scanf("%s", s);
        add();
    }
    dfs(0, 0);
    int sum = accumulate(c[0].begin(),c[0].end(), 0);
    printf("%d\n", sum);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值