H. Subsequences (hard version)

这个题目好难啊,根本就不知道怎么dp,看了题解,理解了好一会才会的。

首先dp[i][j] 表示前面 i  个字符,形成长度为 j  的不同子字符串的个数。

dp[i][j]=dp[i-1][j-1]+dp[i][j-1]  这个就是说这个字符选还是不选。

但是需要注意的是,这个会有重复的字符,如果碰到重复的字符了,这样转移就会出现一点问题,这样会多加了一些情况。

比如说 xyzabca  dp[7][2] 就是在前面7个字符里面选长度为2的字符的数量,dp[7][2]=dp[6][1]+dp[6][2]

dp[6][1]转移过来,意味着这个第7个字符一定要选,所以就会有xa yx za这种答案,

但是这种答案在dp[4][2]=dp[3][1]+dp[3][2]这里转移的时候,dp[3][1]就已经包含了这种答案,所以要删去dp[3][1]

总的来说就是如果一个字符x前面已经出现过,那么就要删去以字符x结尾的该长度减1的子序列。

即 dp[i][j]-=dp[pre[i]-1][j-1]

知道这些了就可以敲代码了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>
#include <iostream>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=2e5+;
ll dp[][];
int pre[];
char s[maxn]; int main(){
ll n,k;
scanf("%lld%lld",&n,&k);
scanf("%s",s+);
dp[][]=;
for(int i=;i<=n;i++){
dp[i][]=;
for(int j=;j<=i;j++){
dp[i][j]=dp[i-][j-]+dp[i-][j];
if(pre[s[i]-'a']) dp[i][j]-=dp[pre[s[i]-'a']-][j-];
dp[i][j]=min(dp[i][j],k);
}
pre[s[i]-'a']=i;
}
ll sum=,ans=;
bool flag=;
for(int i=n;i>=;i--){
if(sum+dp[n][i]>=k){
flag=;
ans+=(n-i)*1ll*(k-sum);
break;
}
sum+=dp[n][i];
ans+=(n-i)*dp[n][i];
}
if(flag) printf("%lld\n",ans);
else printf("-1\n");
return ;
}

H. Subsequences (hard version) dp的更多相关文章

  1. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

    F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...

  2. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  3. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  4. G - Pictures with Kittens (easy version) dp

    https://codeforces.com/problemset/problem/1077/F1 这个其实是一个比较简单的dp了 题目大意: 给你n个数,让你从n个数里选出x个数,并且每隔k个至少选 ...

  5. CodeForces - 1183H Subsequences (hard version) (DP)

    题目:https://vjudge.net/contest/325352#problem/C 题意:输入n,m,给你一个长度为n的串,然后你有一个集合,集合里面都是你的子序列,集合里面不能重复,集合中 ...

  6. HDU 5119 Happy Matt Friends(2014北京区域赛现场赛H题 裸背包DP)

    虽然是一道还是算简单的DP,甚至不用滚动数组也能AC,数据量不算很大. 对于N个数,每个数只存在两个状态,取 和 不取. 容易得出状态转移方程: dp[i][j] = dp[i - 1][j ^ a[ ...

  7. Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】

    H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...

  8. UVa 10069 Distinct Subsequences(大数 DP)

     题意 求母串中子串出现的次数(长度不超过1后面100个0  显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数   当a[i]==b[j]&am ...

  9. CodeForces - 1183E Subsequences (easy version) (字符串bfs)

    The only difference between the easy and the hard versions is constraints. A subsequence is a string ...

随机推荐

  1. Java面向对象的总结

    面向对象的程序设计 1.面向对象 核心:以类的方式组织代码,以对象的方式封装数据 比喻:也就是说类是没有数据的,给了数据之后的类就是对象 封装 继承 多态 2.方法 a.一个方法只有一个返回值,只有一 ...

  2. 解决从服务器获取的数组是 __NSCFConstantString以及""没有空格字符串的问题

    AJ分享,必须精品 问题 项目遇到了个bug,从服务器获取到的数据是这样的 { status = 1, data = [ { uid = 161, type = 2, id = 79, addtime ...

  3. 让ul li水平居中(任意删除li也能水平居中)

    HTML代码: <div class="box"> <ul class="button-ct"> <li></li&g ...

  4. python高级特性之封包与解包

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:kwsy PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...

  5. Co-prime 杭电4135

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  6. api_DZFPKJ & api_DZFPCX

    AES加密算法的网站:http://www.ssleye.com/aes_cipher.html """ AES加密(加解密算法/工作模式/填充方式:AES/ECB/PK ...

  7. 3. css百度制作字体图片

    http://fontstore.baidu.com/static/editor/index.html?qq-pf-to=pcqq.group

  8. python-用户输入和while循环

    函数input() 比较大小要同类型: age=iput() 21 age=int(age) age>=10 true prompt = "If you tell us who you ...

  9. Selenium常见报错问题(2)- 解决和分析StaleElementReferenceException异常

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  10. iscsi的工作原理与优化(2)

    2.1 iSCSI协议模型,iscsi[会话层协议,即应用协议] iSCSI使用TCP/IP协议在不稳定网络上进行可靠的数据传输.iSCSI层和标准SCSI集在协议栈中的位置如图1所示.iSCSI层包 ...