codeforces 1183H 动态规划

传送门:https://codeforces.com/contest/1183/problem/H

题意:

给你一串长度为n的字符串,你需要寻找出他的最长的前k个子串,问你得到这些子串需要减少的字符个数之和是多少,easy版本的k是100,hard版本的k是1e12。

题解:

hard版本题解:

dp[i][j]表示前i个字符中选择了j个的子串数目

如果前面有出现过的字符呢?比如 aba 算到第二个a的时候 把ab 删掉 和 把 ba删掉 得到同一种结果

因此,对于XYYX 这种形式的我们把第一个X之前出现过的种数去掉,就可以避免多算的情况了

这里我们用一个pre数组来记录第一个X出现的位置

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
LL dp[1005][1005];
char a[maxn];
int pre[1005];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
IO;
int n;
LL k;
scanf("%d%lld%s", &n, &k, a + 1);
dp[0][0] = 1;
for(int i = 1; i <= n; i++) {
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[a[i] - 'a']) dp[i][j] -= dp[pre[a[i] - 'a'] - 1][j - 1];
if(dp[i][j] > k) dp[i][j] = k;
}
pre[a[i] - 'a'] = i;
}
LL ans = 0;
for(int i = n; i >= 0; i--) {
ans += min(dp[n][i], k) * (n - i);
k -= dp[n][i];
if(k <= 0) break;
}
if(k > 0) printf("-1\n");
else printf("%lld\n", ans);
return 0;
}

codeforces 1183H 动态规划的更多相关文章

  1. Codeforces 837D 动态规划

    Codeforces 837D 动态规划 传送门:https://codeforces.com/contest/837/problem/D 题意: 给你n个数,问你从这n个数中取出k个数,这k个数的乘 ...

  2. Educational Codeforces Round 21 Problem E(Codeforces 808E) - 动态规划 - 贪心

    After several latest reforms many tourists are planning to visit Berland, and Berland people underst ...

  3. CODEFORCES 429B 动态规划

    http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...

  4. codeforces#1183H. Subsequences(字符串dp)

    题目链接: http://codeforces.com/contest/1183/problem/H 题意: 给出一个长度为$n$的字符串,得到$k$个子串,子串$s$的花费是$n-|s|$ 计算最小 ...

  5. CodeForces 366C 动态规划 转化背包思想

    这道题目昨晚比赛没做出来,昨晚隐约觉得就是个动态规划,但是没想到怎么DP,今天想了一下,突然有个点子,即局部最优子结构为 1-j,j<i,遍历i,每次从所有的1到j当中的最优解里面与当前商品进行 ...

  6. Codeforces 607A 动态规划

    A. Chain Reaction time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

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

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

  8. Codeforces 1183H DP 计算子序列数目

    题意及思路:https://blog.csdn.net/mmk27_word/article/details/93999633 第一次见这种DP,有点像退背包的思想,如果发现有可能因为字母相同和前面算 ...

  9. Comb CodeForces - 46E (动态规划)

    题面 Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To h ...

随机推荐

  1. app被Rejected 的各种原因翻译。这个绝对有用

    1. Terms and conditions(法律与条款) 1.1  As a developer of applications for the App Store you are bound b ...

  2. LeedCode OJ -- String to Integer (atoi)

    点击打开题目链接 题目意思就是自己实现一个atoi函数,也就是将字符串转换成int型. 关于INT_MAX和INT_MIN, 只是在<limits.h>文件中定义的宏..分别是int型可以 ...

  3. LeetCode110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

    POJ 1845 题意不说了,网上一大堆.此题做了一天,必须要整理一下了. 刚开始用费马小定理做,WA.(poj敢说我代码WA???)(以下代码其实都不严谨,按照数据要求A是可以等于0的,那么结果自然 ...

  5. HTTP Status 500 - java.lang.ClassNotFoundException: org.apache.jsp.register_jsp

    你搜一下你的页面中是不是有<!---->的注释 去掉就好了 改成jsp的注释 1).JSP页面中的HTML注释 JSP页面中的HTML注释使用“<!—”和“-->”创建,它的具 ...

  6. 无人驾驶——对frenet坐标的理解

    好的确定车和路之间的关系,我们通常将车辆的在大地坐标坐标转化为车辆和道路之间的frenet坐标. 可能有人会疑问为什么转换后就方便了呢?我们来看一个例子. 在大地坐标下: 无人车首先要知道红色车的位置 ...

  7. C++笔记:面向对象编程(Handle类)

    句柄类 句柄类的出现是为了解决用户使用指针时须要控制指针的载入和释放的问题. 用指针訪问对象非常easy出现悬垂指针或者内存泄漏的问题. 为了解决这些问题,有很多方法能够使用,句柄类就是当中之中的一个 ...

  8. SSH基本原理

    SSH原理与运用:远程登录 作者: 阮一峰 年12月21日 SSH是每一台Linux电脑的标准配置. 随着Linux设备从电脑逐渐扩展到手机.外设和家用电器,SSH的使用范围也越来越广.不仅程序员离不 ...

  9. Oracle ltrim() 函数用法

    Oracle ltrim() 函数用法 2015-03-21 20:42:40 Je_WangZhe 阅读数 8834更多 分类专栏: Oracle   版权声明:本文为博主原创文章,遵循CC 4.0 ...

  10. pytorch更新

    Pytorch如何更新版本与卸载,使用pip,conda更新卸载Pytorch 2018年05月22日 07:33:52 醉雨轩Y 阅读数 19047   今天我们主要汇总如何使用使用ubuntu,C ...