Display Substring

       When we display a string on a LED screen, there may be different energy consumptions for displaying different characters. Given a string S with length n consisting of lowercase English letters and the energy consumption of each letter. Assume that the energy consumption of a string is the sum of the energy consumptions of all its characters. Please find the k-th smallest energy consumption of all the different substrings of S. Note that a substring of a string S is a contiguous subsequence of S. We say two strings S and T are different if the lengths of them are different or there exists at least one position i satisfying S[i] ̸= T[i].

Input

The first line of the input contains an integer T (1 ≤ T ≤ 2 × 103 ) — the number of test cases. The first line of each test case contains two integers n (1 ≤ n ≤ 105 ) and k (1 ≤ k ≤ n(n+1) 2 ). The second line of each test case contains a string S with length n consisting of lowercase English letters. The third line of each test case contains 26 integers ca, cb, . . . , cz (1 ≤ cα ≤ 100) — the energy consumption of each letter. It is guaranteed that the sum of n among all test cases does not exceed 8 × 105 .

Output

For each test case, output a single integer in a separate line — the k-th smallest energy consumption, or −1 if there is no answer.

#include <bits/stdc++.h>
using namespace std;

const int N = 100000 + 9;

char s[N];
int n, c[26];
long long k;

template <int N, int sigma> struct Suffix_Automaton {
    int tot, last, nxt[N * 2][sigma], len[N * 2], link[N * 2];
    int sum[N], pos[N * 2];
    vector<int> ch[N * 2];
    void init(int n) {
        tot = last = 0; link[0] = -1;
        memset(nxt, 0, (n * 2 + 5) * sigma * sizeof(int));
        memset(pos, 0, (n * 2 + 5) * sizeof(int));
        for (int i = 0; i < n * 2 + 5; ++i) ch[i].clear();
    }
    void add_char(int c) {
        int p = last, cur = last = ++tot; len[cur] = len[p] + 1;
        while (~p && !nxt[p][c]) { nxt[p][c] = cur; p = link[p]; }
        if (p == -1) { link[cur] = 0; return; }
        int q = nxt[p][c];
        if (len[q] == len[p] + 1) { link[cur] = q; return; }
        int _q = ++tot; len[_q] = len[p] + 1;
        memcpy(nxt[_q], nxt[q], sigma * sizeof(int));
        link[_q] = link[q]; link[q] = link[cur] = _q;
        while (~p && nxt[p][c] == q) { nxt[p][c] = _q; p = link[p]; }
    }
    void dfs(int u) {
        for (int v : ch[u]) dfs(v);
        if (!pos[u]) pos[u] = pos[ch[u][0]];
    }
    void add_string(const char s[], int n) {
        for (int i = 1; i <= n; ++i) {
            sum[i] = sum[i - 1] + c[s[i] - 'a'];
            add_char(s[i] - 'a');
            pos[last] = i;
        }
        for (int i = 1; i <= tot; ++i) {
            ch[link[i]].push_back(i);
        }
        dfs(0);
    }
    bool check(int C) {
        long long cnt = 0;
        for (int i = 1; i <= tot; ++i) {
            int P = pos[i], L = P - len[i] + 1, R = P - len[link[i]];
            while (L < R) {
                int M = (L + R) >> 1;
                if (sum[P] - sum[M - 1] <= C) R = M;
                else L = M + 1;
            }
            if (sum[P] - sum[L - 1] <= C) cnt += P - len[link[i]] - L + 1;
        }
        return cnt >= k;
    }
};

Suffix_Automaton<N, 26> sam;

void solve() {
    scanf("%d %lld", &n, &k);
    scanf("%s", s + 1);
    for (int i = 0; i < 26; ++i) {
        scanf("%d", c + i);
    }
    sam.init(n);
    sam.add_string(s, n);
    int L = 1, R = sam.sum[n];
    while (L < R) {
        int M = (L + R) >> 1;
        if (sam.check(M)) R = M;
        else L = M + 1;
    }
    if (sam.check(L)) printf("%d\n", L);
    else printf("-1\n");
}

int main() {
    int T; scanf("%d", &T);
    while (T--) solve();
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值