Google Kickstart 2020 Round A Bundling(字典树)

Problem
Pip has N strings. Each string consists only of letters from A to Z. Pip would like to bundle their strings into groups of size K. Each string must belong to exactly one group.

The score of a group is equal to the length of the longest prefix shared by all the strings in that group. For example:
The group {RAINBOW, RANK, RANDOM, RANK} has a score of 2 (the longest prefix is ‘RA’).
The group {FIRE, FIREBALL, FIREFIGHTER} has a score of 4 (the longest prefix is ‘FIRE’).
The group {ALLOCATION, PLATE, WORKOUT, BUNDLING} has a score of 0 (the longest prefix is ‘’).

Please help Pip bundle their strings into groups of size K, such that the sum of scores of the groups is maximized.

Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the two integers N and K. Then, N lines follow, each containing one of Pip’s strings.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum sum of scores possible.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ N ≤ 105.
2 ≤ K ≤ N.
K divides N.
Each of Pip’s strings contain at least one character.
Each string consists only of letters from A to Z.

Test set 1
Each of Pip’s strings contain at most 5 characters.

Test set 2
The total number of characters in Pip’s strings across all test cases is at most 2 × 106.

Samples

Input 1

Output 1

2
2 2
KICK
START
8 2
G
G
GO
GO
GOO
GOO
GOOO
GOOO

Case #1: 0
Case #2: 10

Input 2

Output 2

1
6 3
RAINBOW
FIREBALL
RANK
RANDOM
FIREWALL
FIREFIGHTER

Case #1: 6

Sample #1
In Case #1, Pip can achieve a total score of 0 by make the groups:
{KICK, START}, with a score of 0.

In Case #2, Pip can achieve a total score of 10 by make the groups:
{G, G}, with a score of 1.
{GO, GO}, with a score of 2.
{GOO, GOO}, with a score of 3.
{GOOO, GOOO}, with a score of 4.

Sample #2
In Case #1, Pip can achieve a total score of 6 by make the groups:
{RAINBOW, RANK, RANDOM}, with a score of 2.
{FIREBALL, FIREWALL, FIREFIGHTER}, with a score of 4.

Note #1: Only Sample #1 is a valid input for Test set 1. Consequently, Sample #1 will be used as a sample test set for your submissions.
Note #2: Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.

思路:
没写出来,唉。
看到字符串前缀,想到了AC自动机,我就纳闷了这帮人字符串怎么这么好(狗头。
后面听学长讲字典树,又想了一下,这尼玛不是直接统计前缀数目然后除以k算贡献吗?

统计完前缀数目,直接除以k,对就是这么粗暴。

代码写的很粗暴,理论上每次初始化你可以使用dfs,只清空赋了值的节点

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <string>
#include <iostream>

using namespace std;

typedef long long ll;

const int maxn = 2e6 + 7;

string a[maxn];
int t[maxn][26];
int val[maxn];
int tot;

void insert(string s) {
    int u = 0;
    for(int i = 0;i < s.size();i++) {
        int id = s[i] - 'A';
        if(!t[u][id]) {
            t[u][id] = ++tot;
        }
        val[t[u][id]]++;
        u = t[u][id];
    }
}

int main() {
    int T;scanf("%d",&T);
    int kase = 0;
    while(T--) {
        tot = 0;
        memset(val,0,sizeof(val));
        memset(t,0,sizeof(t));
        int n,k;scanf("%d%d",&n,&k);
        for(int i = 1;i <= n;i++) {
            cin >> a[i];
            insert(a[i]);
        }
        
        int ans = 0;
        for(int i = 1;i <= tot;i++) {
            ans += val[i] / k;
        }
        
        printf("Case #%d: %d\n",++kase,ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值