codeforces 1183 H. Subsequences (hard version)(dp求不重复子序列数量)

H. Subsequences (hard version)

The only difference between the easy and the hard versions is constraints.

A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".

You are given a string ss consisting of nn lowercase Latin letters.

In one move you can take any subsequence tt of the given string and add it to the set SS. The set SS can't contain duplicates. This move costs n−|t|n−|t|, where |t||t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).

Your task is to find out the minimum possible total cost to obtain a set SS of size kk or report that it is impossible to do so.

Input

The first line of the input contains two integers nn and kk (1≤n≤100,1≤k≤10121≤n≤100,1≤k≤1012) — the length of the string and the size of the set, correspondingly.

The second line of the input contains a string ss consisting of nn lowercase Latin letters.

Output

Print one integer — if it is impossible to obtain the set SS of size kk, print -1. Otherwise, print the minimum possible total cost to do it.

Examples

input

4 5
asdf

output

4

input

5 6
aaaaa

output

15

input

5 7
aaaaa

output

-1

input

10 100
ajihiushda

output

233

Note

In the first example we can generate SS = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in SS is 00 and the cost of the others is 11. So the total cost of SS is 44.

 

题意:给一个字符串 s , 获得 s 中的一个子序列 t 花费是 |s| - |t| , 问最后得到 k 个不同的子序列的最小花费。

思路:dp[i][j] 表示前 i 个字符删除 j 个所得到的子序列数目。

状态转移方程 dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] 分别表示保不保留第 i 个字符。

但是这样会有重复的子序列,比如 abcdefgxyx 在删除 xy 和 yx 之后得到是相同的子序列。

那么有 pre 表示上一个此字符出现的位置。当 j >= i - pre (i - pre 即把中间部分删掉,也就是把上面的 xy 删掉) 时就会有重复的子序列...即重复 dp[pre - 1][pre - (i - j)],所以要减去

Code:

#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define INT(t) int t; scanf("%d",&t)

using namespace std;

const int maxn = 110;
LL dp[maxn][maxn];
int pre[30];
char s[maxn];

int main() {
    int n; LL k;
    while(~scanf("%d%lld",&n,&k)){
        scanf("%s",s + 1);
        clr(pre,0);
        dp[0][0] = 1LL;
        for(int i = 1;i <= strlen(s + 1);++ i){
            dp[i][0] = 1LL;
            for(int j = 1;j <= i;++ j){
                dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                int dd = pre[s[i] - 'a'];
                if(dd && j >= i - dd)
                    dp[i][j] -= dp[dd - 1][dd - (i - j)];
                dp[i][j] = min(dp[i][j],k);
            }
            pre[s[i] - 'a'] = i;
        }
        LL ans = 0;
        for(int i = 0;i <= n;++ i){
            ans += min(k,dp[n][i]) * i;
            k -= dp[n][i];
            if(k <= 0) break;
        }
        if(k > 0) printf("-1\n");
        else cout << ans << endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/rookie-acmer/p/11262096.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值