Codeforces 1083B The Fair Nut and Strings
Description
给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\)
\(n\) 表示字符串 \(a\),\(b\) 的长度, 要求你最多 选 \(k\) 个 字符串 \(t_i\) 满足 \(a<=t_i<=b\), 并且使得这些字符串的前缀的数量最大
Solution
很妙的解法。
所有可以选择的字符串可以看成一棵字典树。
我们发现, 如果第\(i\)层的节点数 \(<=k\), 那么这一层的前缀都可以加入集合
反之, 第\(i\)层的节点数 \(>k\), 那么这一层的前缀最多有\(k\)个加入集合。这些前缀长度为 \(i\) 且互不相等
所以只需要算出每一层节点的个数并加入贡献, 就能得到答案
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define up(a, b) (a = a > b ? a : b)
#define down(a, b) (a = a > b ? b : a)
#define cmax(a, b) (a > b ? a : b)
#define cmin(a, b) (a > b ? b : a)
#define Abs(a) ((a) > 0 ? (a) : -(a))
#define rd read()
#define db double
#define LL long long
using namespace std;
LL read() {
LL X = 0, p = 1; char c = getchar();
for (; c > '9' || c < '0'; c = getchar())
if (c == '-') p = -1;
for (; c >= '0' && c <= '9'; c = getchar())
X = X * 10 + c - '0';
return X * p;
}
const int N = 5e5 + 5;
char a[N], b[N];
int main()
{
int n = rd, k = rd;
scanf("%s%s", a + 1, b + 1);
LL ans = 0, tmp = 1;
for (int i = 1; i <= n; ++i) {
tmp *= 2;
if (b[i] == 'a')
tmp--;
if (a[i] == 'b')
tmp--;
if (tmp > k)
tmp = k + 1;
ans += cmin(tmp, k);
}
printf("%lld\n", ans);
}
Codeforces 1083B The Fair Nut and Strings的更多相关文章
- Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings
E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...
- CF 1083 B. The Fair Nut and Strings
B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析: 建出trie树,给定的两个字符串就 ...
- CodeForces 1084D The Fair Nut and the Best Path
The Fair Nut and the Best Path 题意:求路径上的 点权和 - 边权和 最大, 然后不能存在某个点为负数. 题解: dfs一遍, 求所有儿子走到这个点的最大值和次大值. 我 ...
- Codeforces 1083E The Fair Nut and Rectangles
Description 有\(N\)个左下定点为原点的矩阵, 每个矩阵\((x_i,~y_i)\)都有一个数\(a_i\)表示其花费. 没有一个矩阵包含另一个矩阵. 现要你选出若干个矩阵, 使得矩阵组 ...
- 【贪心/Trie】【CF1083B】 The Fair Nut and Strings
Description 有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\ ...
- [CF1083B]The Fair Nut and Strings
题目大意:在给定的长度为$n(n\leqslant5\times10^5)$的字符串$A$和字符串$B$中找到最多$k$个字符串,使得这$k$个字符串不同的前缀字符串的数量最多(只包含字符$a$和$b ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path
D. The Fair Nut and the Best Path 题目链接:https://codeforces.com/contest/1084/problem/D 题意: 给出一棵树,走不重复的 ...
- Codeforces Round #526 (Div. 2) C. The Fair Nut and String
C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
随机推荐
- start to use await
import { browser, $, $$, by, element, protractor } from "protractor"; //import { SearchPag ...
- JAVA的第二次作业
1.编写“人”类及其测试类.1.1 “人”类: 类名:Person 属性:姓名.性别.年龄.身份证号码 方法:在控制台输出各个信息1.2 测试类 类名:TestPerson 方法:main ...
- C++重写new和delete,比想像中困难
关于C++内存管理这话题,永远都不过时.在我刚出道的时候,就已经在考虑怎么检测内存泄漏(https://www.cnblogs.com/coding-my-life/p/3985164.html).想 ...
- Android 7.0 通过FileProvider共享文件
一.概述 Android 7.0后,提供了很多新特性,其中最主要的是禁止了通过file://URI直接在文件操作共享文件(该操作会触发FileUriExposedException),而是通过cont ...
- 把http网站变成https网站
所需条件: 一个网站域名,必须是注册的合法域名,国内域名最好已经通过备案: 一个空间,可以是虚拟空间或者云主机: 一张SSL证书. 操作步骤: 域名注册.国内的有万网和国外GoDaddy,当然域名注册 ...
- 在Ubuntu上升级SQLite,并让Python使用新版SQLite
(本文适用于Debian系的Linux,如Ubuntu.Raspbian等等.) 在Linux上,Python的sqlite3模块使用系统自带的SQLite引擎,然而系统自带的SQLite可能版本太老 ...
- 让新版appium支持by_name定位
org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this sess ...
- 使用pip安装包提示TLS证书错误解决办法
最近有在使用pip安装python包的时候,总会出现以下类似的错误: Could not fetch URL https://pypi.python.org/simple/pytest-xdist/: ...
- vo与po
场景: 控制层使用vo对象,数据层使用po对象,业务层负责将po对象转换成vo对象传递给控制层 vo和po对象之间转换可以用BeanUtils.copyProperties(vo, po);方法 如果 ...
- IDEA中MAVEN项目Dependency not found 问题
STEP1: 更新IDEA保存的仓库索引.操作步骤File->Setting->Maven->Repositories->Update.dependecy not found问 ...