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 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
随机推荐
- eclipse 安装MyBatis插件 -- 官网直接拖动“安装”
拖动“安装” http://marketplace.eclipse.org/marketplace-client-intro?mpc_install=2947754
- bootstrapvalidator的简单校验【必填校验、长度校验、是否存在校验(remote)】
需求:输入框的"供应商编码"不能为空而且不能与数据库重复,供应商名称不能为空. 解决: 1.input如下 <input id="ssupplierNo" ...
- gentoo qt-creator no examples
参考 [SOLVED] qtcreator: examples are missing https://forums.gentoo.org/viewtopic-t-1092826-highlight- ...
- expdp/impdp数据泵分区表导入太慢了。添加不检查元数据参数提高效率:ACCESS_METHOD=DIRECT_PATH
分区表数据泵导入太慢,达不到客户的迁移要求导出语句如下:(10G单节点)userid='/ as sysdba'directory=milk_dirdumpfile=mon_%U.dmplogfile ...
- Visual Studio中定义OVERFLOW不能用
在Visual Studio中对OK.ERROR.OVERFLOW进行宏定义,但只有OVERFLOW不能正常使用为什么呢? #define OK 1: #define ERROR 0: #define ...
- Windows2008R2操作系统日志清理
Windows日志路径 c:/windows/system32/winevt/logs
- 2522 和为K的倍数
2522 和为K的倍数 2 秒 262,144 KB 10 分 2 级题 小b喜欢和为K的倍数的序列. 现在有一个长度为n的序列A,请问A有多少个非空连续子序列是小b喜欢的. 收起 输入 第一 ...
- SpringMVC参数注解解释
在Controller的开发中,经常会用到很多注解,下面解释一下关于形参常用注解的一些解释,他们主要是用来接收参数的. @ModelAttribute("vo", ObjectVo ...
- visual studio 2013 几个测试工具(Nunit 3、xUnit)
一.Nunit 3 1.在解决方案里添加一个类库——引用——右键(如下图)) 3.搜索nunit 并安装(如图) 3.注意引入命名空间并给测试类和测试方法添加特性(如图) 4.如果测试通过则为绿色(如 ...
- nginx配置location总结及rewrite规则写法(转)
一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...