Codeforces 1183H Subsequences (hard version)


传送门:Codeforces 1183H


Subsequences (hard version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description
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 s consisting of n lowercase Latin letters.
In one move you can take any subsequence t of the given string and add it to the set S. The set S can’t contain duplicates. This move costs n−|t|, where |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 S of size k or report that it is impossible to do so.

Input
The first line of the input contains two integers n and k (1≤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 s consisting of n lowercase Latin letters.

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

Sample Input
4 5
asdf
5 6
aaaaa
5 7
aaaaa
10 100
ajihiushda

Sample Output
4
15
-1
233




题意:
给出一个字符串删除一些字符使得子串个数是K,并且代价最小,代价为删除的字符数量。


题解:
dp[i][j]代表从前i个中选出长度为j的子序列的总数(无重复);
如果不考虑重复很自然的想到一个转移方程:
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];

那么重复的话,就应该是以s[i]为结尾的子序列重复了,那么我也只要找当前位置之前第一个与之相同的数在哪就行了(因为之前的已经删过了)。



AC代码:

 #include<iostream>
 #include<algorithm>
 #include<cstring>
 #include<cstdio>
 #include<cmath>
 #define ll long long
 #define inf 0x3f3f3f3f
 using namespace std;
 char s[110];
 ll dp[110][110];//dp[i][j]表示前i个选长度为j的子序列,共有多少个无重复的子序列
 ll n,k;
 int pre[27];//存之前同样字符的位置
 int main()
 {
   scanf("%I64d%I64d",&n,&k);
   scanf("%s",s+1);
   memset(pre,0,sizeof(pre));
   dp[0][0]=1;
   for(int i=1;i<=n;i++)
   {
     int alp=s[i]-'a';//当前字母
     dp[i][0]=1;//不选,即空集,只有一个
     for(int j=1;j<=i;j++)
     {
       dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
       if(pre[alp])//之前出现过同样字符
       {
         dp[i][j]-=dp[pre[alp]-1][j-1];
       }
       dp[i][j]=min(dp[i][j],k);
     }
     pre[alp]=i;
   }
   ll cost=0;
   for(int i=n;i>=0;i--)
   {
     cost+=min(dp[n][i],k)*(n-i);
     k-=min(dp[n][i],k);
     if(k==0) break;
   }
   if(k>0) printf("-1\n");
   else printf("%I64d\n",cost);
   return 0;
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值