原题出处:http://codeforces.com/contest/1183/problem/H
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
Copy
4 5 asdf
output
Copy
4
input
Copy
5 6 aaaaa
output
Copy
15
input
Copy
5 7 aaaaa
output
Copy
-1
input
Copy
10 100 ajihiushda
output
Copy
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.
由于k值很大,故不能再用bfs了,可以考虑用dp来解决
先来看一看另外一个简单的问题,如果不考虑子序列长度,一个给定的字符串我们如何得知其所有子
序列个数呢?假设从字符串str长度为len,下标从1~len,设f[i]为考虑前 i 个字符的答案,那么
f[i] = f[i-1]*2 ; (第 i 个字符从未出现)
f[i] = f[i-1]*2 - f[last[str[i]]-1] ; (第 i 个字符出现过,last[]为记录该字符上一次出
现的位置)
"*2"很显然表示的是添加这个字符或不加,然而有重复的子序列在其中,需要减去这个字符上一次作为结
尾的序列,"-1"便是该字符上一个位置之前的所有子序列并加上它结尾,没有"-1”则多减去不以它结尾的
子序列。好的,了解这个经典的问题后,我们再来解决这个 hard version ,现在我们给dp的状态额外加
一个维度表示子序列长度,f[i][j] 即是考虑前 i 个字符,长为 j 的子序列。
f[i][j] = f[i-1][j-1] + f[i-1][j] ; (第 i 个字符从未出现)
f[i][j] = f[i-1][j-1] + f[i-1][j] - f[last[str[i]]-1][j-1] ; (第 i 个字符出现过,
last[]为记录该字符上一次出现的位置)
答案就从 f[len][len ~ 0] 里找前 K 个进行计算,BTW为了防止数字过大溢出,在转移的时候加
一个 min (K , f[i][j]); 这样可防止溢出并保证答案正确 。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxm = 110;
ll dp[maxm][maxm];
int last[30];
char s[maxm];
int main()
{
int n;
ll k;
scanf("%d %lld",&n,&k);
scanf("%s",s);
dp[0][0] = 1;
for (int i=1;i<=n;i++)
{
dp[i][0] = 1;
for (int j=1;j<=i;j++)
{
if (last[s[i-1]-'a'])
dp[i][j] = min(dp[i-1][j-1]+dp[i-1][j]-
dp[last[s[i-1]-'a']-1][j-1],k);
else
dp[i][j] = min(k,dp[i-1][j-1] + dp[i-1][j]);
}
last[s[i-1]-'a'] = i;
}
ll ans = 0;
for (int i=n;i>=0;i--)
{
ll t = min(k,dp[n][i]);
ans += t * (n - i);
k -= t;
}
if (k!=0) ans = -1;
printf("%lld\n",ans);
return 0;
}