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的所有连续子串中,找出这些 ...
随机推荐
- 推荐一款在IntelliJ IDEA中使用微信/QQ的插件
SmartIM SmartIM4IntelliJ 是一个 IntelliJ IDEA 上的 SmartIM(原 SmartQQ)插件,可以在 IDEA 中使用 QQ 或微信聊天. 功能 收发文本消息 ...
- C#百度api 根据经纬度获取地址
public string GetAddress(string lat, string lng) { try { string res = ""; string url = @&q ...
- Shell 脚本来自动监控 Linux 系统的内存
# vim /scripts/swap-warning.sh #!/bin/bash #提取本服务器的IP地址信息 IP=`ifconfig eth0 | grep "inet addr&q ...
- POJ3336 Making the Grade
思路:DP 提交:1次 题解: 最开始我们可以想到,分两种序列都做一遍. 先来证明一个结论: 存在一种构造,使 \(B\) 中的数都在 \(A\) 中出现过,且这样不劣. (目的是为了转化暂时看起来虚 ...
- 015_STM32程序移植之_NRF24L01模块
STM32程序移植之NRF24L01模块 引脚接线图如下所示 STM32引脚 NRF24L01引脚 功能 GND GND 3.3V 3.3V PB8 CE PB9 CSN PB13 SCK PB15 ...
- Codevs 1038 一元三次方程求解 NOIP 2001(导数 牛顿迭代)
1038 一元三次方程求解 2001年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 有形如:ax3+b ...
- unix/linux 进程间文件锁
转自 http://www.cnblogs.com/hjslovewcl/archive/2011/03/14/2314333.html 有三种不同的文件锁,这三种都是“咨询性”的,也就是说它们依靠程 ...
- D. Restore Permutation(权值线段树)
D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- HDU 1087 Super Jumping! Jumping! Jumping! ——(LIS变形)
和之前那个长方体最大高度是换汤不换药的题目.每次找之前最大的一个能接的接上即可.代码如下: #include <stdio.h> #include <algorithm> #i ...
- ArcGIS超级工具SPTOOLS-拓扑错误处理
1.1 删除线面直线上的点 操作视频: https://weibo.com/tv/v/Hxjgmuv6F?fid=1034:4379388532225679 删除面要素.线要素一条边直线上的点. 1 ...