Codeforces 965E 启发式合并

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;
}

猜你喜欢

转载自blog.csdn.net/qq_36876305/article/details/80111959
今日推荐