CF1183E/H Subsequences
思路:
dp好题,dp[i][j]表示到前i个字符为止并且以s[i]为结尾,共有多少个长度为j的不同的子序列。
实现:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[][], sum[];
int last[];
int main()
{
int n; ll m; string s;
while (cin >> n >> m >> s)
{
memset(dp, , sizeof dp);
memset(sum, , sizeof sum);
memset(last, -, sizeof last);
for (int i = ; i <= n; i++)
{
dp[i][] = ;
for (int j = ; j <= i; j++)
{
for (int k = ; k < ; k++)
{
if (last[k] != -)
{
int p = last[k];
dp[i][j] += dp[p][j - ];
}
}
}
last[s[i - ] - 'a'] = i;
}
for (int i = ; i <= n; i++)
{
for (int j = ; j < ; j++)
{
if (last[j] != -) sum[i] += dp[last[j]][i];
}
}
sum[] = ;
ll ans = ;
bool flg = false;
for (int i = n; i >= ; i--)
{
if (m - sum[i] <= ) { ans += m * ((ll)n - i); flg = true; break; }
ans += sum[i] * ((ll)n - i); m -= sum[i];
}
cout << (flg ? ans : -) << endl;
}
return ;
}
CF1183E/H Subsequences的更多相关文章
- H. Subsequences (hard version) dp
H. Subsequences (hard version) 这个题目好难啊,根本就不知道怎么dp,看了题解,理解了好一会才会的. 首先dp[i][j] 表示前面 i 个字符,形成长度为 j 的不 ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 H. Hashing
H. Hashing time limit per test 1 second memory limit per test 512 megabytes input standard input out ...
- CodeForces 689D Friends and Subsequences (RMQ+二分)
Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- H.264视频的RTP荷载格式
Status of This Memo This document specifies an Internet standards track protocol for the Internet ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)
题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...
随机推荐
- idea maven配置
转载自:https://www.cnblogs.com/Silencepeng/p/7444012.html 一.下载maven的包 http://www.apache.org/ 1.在网页中打开上面 ...
- js校验密码必须包含字母大小写、数字
校验密码必须包含字母大小写.数字 function checkPasswordNew(s){ var str=trim(s); //var reg = /^(?![A-Z]+$)(?![a-z]+$) ...
- MySQL 8.0.15 配置 MGR单主多从
转载自:http://www.cnblogs.com/zhangzihong/p/10443526.html 一.简介 MySQL Group Replication(简称MGR)字面意思是mysql ...
- Bzoj 2134: [国家集训队2011]单选错位(期望)
2134: 单选错位 Time Limit: 10 Sec Memory Limit: 259 MB Description Input n很大,为了避免读入耗时太多,输入文件只有5个整数参数n, A ...
- 「CF716D」Complete The Graph「最短路」
题意 给定一个\(n\)个点\(m\)条边的无向图,有一些边权暂时为\(0\),你需要分配一个\([1, 10^{18}]\)的数.最终使得\(s\)到\(t\)最短路为\(L\),输出一个可行的分配 ...
- devstack cinder-volume服务状态为down
cinder-manage service list 查看到有一个 xxx状态 Binary Host Zone Status State Updated At RPC Version Object ...
- SpringAOP配置与使用(示例)
1.pom.xml追加 spring-aspects aspectjrt 为控制器以外的类织入切面 2.新建spring-aop.xml <?xml version="1.0" ...
- easyui复选框实现单选框
$(':checkbox[name=primary_key_flag]').each(function(){ $(this).click(function(){ if(this.checked){ $ ...
- 浅谈 es6 箭头函数, reduce函数介绍
今天来谈一下箭头函数, es6的新特性 首先我们来看下箭头函数长什么样子, let result = (param1, param2) => param1+param2; 上述代码 按照以前书写 ...
- elasticsearch文档冲突
https://www.elastic.co/guide/cn/elasticsearch/guide/current/optimistic-concurrency-control.html当我们之前 ...