思路:

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的更多相关文章

  1. H. Subsequences (hard version) dp

    H. Subsequences (hard version) 这个题目好难啊,根本就不知道怎么dp,看了题解,理解了好一会才会的. 首先dp[i][j] 表示前面 i  个字符,形成长度为 j  的不 ...

  2. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  4. 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 ...

  5. CodeForces 689D Friends and Subsequences (RMQ+二分)

    Friends and Subsequences 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/H Description Mi ...

  6. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  7. H.264视频的RTP荷载格式

    Status of This Memo This document specifies an Internet standards track protocol for the   Internet ...

  8. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  9. Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

    题目链接: Subsequences in Substrings Kattis - subsequencesinsubstrings 题目大意:给你字符串s和t.然后让你在s的所有连续子串中,找出这些 ...

随机推荐

  1. login.exp

    #!/usr/bin/expect ] ] ] ] spawn ssh -p $user@$host expect { "*yes/no*" {send "yes\r&q ...

  2. paramiko远程上传下载文件

    import paramiko import sys user = "root" pwd = " # 上传文件 def sftp_upload_file(server_p ...

  3. 002_linuxC++_.h和.c文件

    (一)程序修改001_linuxC++之_类的引入 (二)修改成为.h和.c文件 #include <stdio.h> #include "person.h" int ...

  4. git error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version

    原因:git 版本过低 解决方法:卸载旧版本,重装新版本

  5. 详解Kafka: 大数据开发最火的核心技术

    详解Kafka: 大数据开发最火的核心技术   架构师技术联盟 2019-06-10 09:23:51 本文共3268个字,预计阅读需要9分钟. 广告 大数据时代来临,如果你还不知道Kafka那你就真 ...

  6. idea快捷方式2

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  7. vscode集成eslint

    1. 安装 ESLint 扩展 首先,打开 VSCode 扩展面板并搜索 ESLint 扩展,然后点击安装 2. 项目安装eslint yarn add eslint -D 3. 设置eslint配置 ...

  8. 检查Object是否存在某个属性

    1. in 和 hasOwnProperty in会检查对象和它的整条原型链,hasOwnProperty只会检查对象本身,不会检查原型链 let a = {name: 'rick'} let b = ...

  9. 报错 One or more constraints have not been satisfied.

    常出现在导入已有标签时. 需要在<build/><plugins/>里面追加标签 <plugin> <groupId>org.apache.maven. ...

  10. c++ 容器切片反转次序(不拷贝到新容器)

    // rotate algorithm example #include <iostream> // cout #include <algorithm> // rotate # ...