题目大意:在给定的长度为$n(n\leqslant5\times10^5)$的字符串$A$和字符串$B$中找到最多$k$个字符串,使得这$k$个字符串不同的前缀字符串的数量最多(只包含字符$a$和$b$)。

题解:考虑给这$k$个字符串建一个$trie$树,答案就是它所含的节点数,考虑贪心,在每一层尽可能多的分叉。注意一层最多只能有$k$个点

卡点:

C++ Code:

#include <cstdio>
#define maxn 500010
int n, k;
long long ans;
char A[maxn], B[maxn];
int main() {
scanf("%d%d", &n, &k);
scanf("%s%s", A, B);
long long now = 1;
for (int i = 0; i < n; ++i) {
now <<= 1;
now -= (A[i] == 'b') + (B[i] == 'a');
if (now >= k) {
printf("%lld\n", ans + static_cast<long long> (n - i) * k);
return 0;
}
ans += now;
}
printf("%lld\n", ans);
return 0;
}

  

[CF1083B]The Fair Nut and Strings的更多相关文章

  1. CF 1083 B. The Fair Nut and Strings

    B. The Fair Nut and Strings 题目链接 题意: 在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多. 分析:  建出trie树,给定的两个字符串就 ...

  2. 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的 ...

  3. 【贪心/Trie】【CF1083B】 The Fair Nut and Strings

    Description 有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\ ...

  4. Codeforces 1083B The Fair Nut and Strings

    Description 给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\) \(n\) 表示字符串 \(a\),\(b\) ...

  5. CF1083B The Fair Nut and String

    题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...

  6. CF1083E The Fair Nut and Rectangles

    CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...

  7. CF 1083 A. The Fair Nut and the Best Path

    A. The Fair Nut and the Best Path https://codeforces.com/contest/1083/problem/A 题意: 在一棵树内找一条路径,使得从起点 ...

  8. CF1083A The Fair Nut and the Best Path

    CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...

  9. 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 题意: 给出一棵树,走不重复的 ...

随机推荐

  1. SpringBoot-05:SpringBoot初运行以及tomcat端口号的修改

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 上篇博客讲了,如何创建SpringBoot工程,所以,我本篇博客讲述,如何跑起来自己的第一个案例 1.准备一个 ...

  2. lesson 16 Mary had a little lamb

    lesson 16 Mary had a little lamb a little + 可数 小的;+ 不可数 少量的 对于动物在幼时都有不同的称呼: calf 小牛 lamb 羊羔 piglet 小 ...

  3. 【WXS全局对象】Global

    Global对象的方法调用时,无需使用 Global.parseInt(...),而是直接使用 parseInt(...) 方法: 名称 说明 parseInt(string, radix) 解析一个 ...

  4. 【RandomString】- 随机字符串

    RandomString  随机字符串的用法

  5. Codeforces 96D Volleyball(最短路径)

    Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...

  6. this指针与const成员函数

    this指针的类型为:classType *const      // 即指向类类型非常量版本的常量指针 所以,我们不能把this绑定到一个常量对象上 ===>  不能在一个常量对象上调用普通的 ...

  7. cp的使用

    一.形式 cp [options] source1 source2 source3 .... directory 参数意义: 参数 意义 -i 当目标文件已存在时,会询问是否覆盖 -p 连同文件的属性 ...

  8. Pipeline组测试说明

    PIPELINE组测试报告 前言:我们组与学霸系统的其他两个小组共同合作开发,组成学霸系统的团体工作.作为学霸系统的一环,我们组起到承上启下的作用,因此,面向群体以及功能实现都是为给下一个组的工作做好 ...

  9. C++读取文件统计单词个数及频率

    1.Github链接 GitHub链接地址https://github.com/Zzwenm/PersonProject-C2 2.PSP表格 PSP2.1 Personal Software Pro ...

  10. PAT L1-044 稳赢

    https://pintia.cn/problem-sets/994805046380707840/problems/994805086365007872 大家应该都会玩“锤子剪刀布”的游戏:两人同时 ...