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. spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

    java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...

  2. re模块语法—python正则表达式

    用字符串匹配实现 对于简单的匹配查找,可以通过字符串匹配实现,比如:查找以”hello”开头的字符串 此时就可以正确查找出以start开始的字符串了 python中的正则表达式模块 在python中为 ...

  3. Python下将一般对象打印成Json

    有的时候,我们在写Python程序的时候,在处理复杂对象的时候,有的时候过程中调试,需要去看看产生的对象如何,有的时候我们可以把它打印成json来看,这个是个不错的办法. 对每一个对象写一个独立的打印 ...

  4. cheat sheet 简介

    cheat sheet 速查表 /小抄 如果期末考试老师只让你让带一张A4纸,合法"作弊",纸上能写多少全凭自己本事,你会写什么?大部分人应该把整个课程的知识重点梳理一遍,方便记忆 ...

  5. DeepinV20系统文件管理器右键发送至为知笔记

    1. 创作背景 昨天在深度系统上做了一个打开文件管理器选择文件右键发送文本至博客园的插件. 这个插件对于我自己来说是及其方便的东西,平时的学习积累,工作经验或者生活感悟,随手记下之后,就能够轻松发送出 ...

  6. Springboot:员工管理之删除员工及退出登录(十(9))

    springboot2.2.6 delete请求报错,降至2.1.11功能可用 原因未知 构建员工删除请求 com\springboot\controller\EmployeeController.j ...

  7. ntp和chrony

    目录 chrony 简介 ntp pool ntp 配置文件 chrony 配置文件 chronyc 命令行工具 修改时区 chrony 简介 chrony 是 RedHat 开发的,它是网络时间协议 ...

  8. hadoop 伪分布配置

    配置 Hadoop 伪分布式 任务配置说明: VMware 15 Centos 6.5 java -jdk 1.8 hadoop-2.6.0-cdh5.14.0.tar.gz 第一步 自行安装虚拟机 ...

  9. NER命名实体识别,实体级level的评估,精确率、召回率和F1值

    pre = "0 0 B_SONG I_SONG I_SONG 0 B_SONG I_SONG I_SONG 0 0 B_SINGER I_SINGER I_SINGER 0 O O O B ...

  10. Data Flow Diagram with Examples - Customer Service System

    Data Flow Diagram with Examples - Customer Service System Data Flow Diagram (DFD) provides a visual ...