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. js遮罩

    1.1 背景半透明遮罩层样式 需要一个黑色(当然也可以其他)背景,且须设置为绝对定位,以下是项目中用到的css样式: /* 半透明的遮罩层 */ #overlay { background: #000 ...

  2. 【Leetcode链表】分隔链表(86)

    题目 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4 ...

  3. 使用 VSCODE 在 Windows 10 WSL 中远程开发

    使用 VSCODE 在 Windows 10 WSL 中远程开发 安装 VSCODE 1.35+ 版本. 在 VSCODE 中安装 WSL 插件. 点击左下角的 WSL 图标. 打开项目,提示路径. ...

  4. 阿里云OSS同城冗余存储正式商业化,提供云上同城容灾能力

    近日,阿里云正式发布OSS同城冗余存储产品.这是国内目前提供同城多AZ冗余部署能力覆盖最广的云上对象存储产品,可以实现云存储的同城双活,满足企业级客户对于“发生机房级灾难事件时数据不丢失,业务不中断” ...

  5. LeetCode93 Restore IP Addresses

    题目: Given a string containing only digits, restore it by returning all possible valid IP address com ...

  6. nodeJs学习-01 http模块

    http模块基础: const http = require("http"); //引入http系统模块 var server = http.createServer(functi ...

  7. oracle函数 MIN([distinct|all]x)

    [功能]统计数据表选中行x列的最大值. [参数]all表示对所有的值求最大值,distinct只对不同的值求最大值,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数] ...

  8. JavaScript原型链中toString()方法输出alert()和console.log()得到不同的结果

    <script language="javascript"> function myObj(){ var total = 0; } myObj.prototype.ad ...

  9. Spring IoC 使用详解

    在Spring中,依赖注入(DI)模式实现了控制反转(IoC)原理.让我们通过一个例子来帮助理解依赖注入.我们先看到java版的例子,然后在此基础上加上spring的功能.就例子而言,是相当地简单.Q ...

  10. selenium webdriver学习(一)------------快速开始(转载JARVI)

    selenium webdriver学习(一)------------快速开始 博客分类: Selenium-webdriver selenium webdriver 学习 selenium webd ...