Description

有 \(k\) 个长度为 \(n\) 的只含 \(a\) 或 \(b\) 字符串,并不知道它们具体是多少,只知道它们的字典序不小于字符串 \(A\),同时不大于字符串 \(B\)。定义一个字符串是合法的当且仅当它是这 \(k\) 个字符串之一的前缀(如果它是多个串的前缀那么只计算一次)。求合法的字符串最大可能是多少

Input

第一行是两个整数 \(n\) 和 \(k\)

下面两行,第一行是长度为 \(n\) 的字符串 \(A\),第二行是长度为 \(n\) 的字符串 \(B\)

Output

输出一个数代表答案。

Hint

\(1~\leq~n~\leq~5~\times~10^5~,~1~\leq~k~\leq~10^9\)

Solution

我们考虑假如对这 \(k\) 个字符串建出一棵踹Trie树,那么显然一个节点对应一个合法的字符串,答案即为树上节点个数。于是我们的问题即被转化为了最大化Trie树上的节点个数。考虑在一个节点,在合法的条件下孩子数为 \(2\) 的答案显然不劣于孩子数为 \(1\) 的答案。于是我们依照此按照层数进行贪心,尽可能的多分节点即可。考虑因为最后一层的节点最多有 \(k\) 个,所以当算到一层的节点数不小于 \(k\) 时,后面就无需枚举,直接分配给每层 \(k\) 个节点计算答案即可。

Code

#include <cstdio>
#include <algorithm>
#ifdef ONLINE_JUDGE
#define freopen(a, b, c)
#endif
#define rg register
#define ci const int
#define cl const long long typedef long long int ll; namespace IPT {
const int L = 1000000;
char buf[L], *front=buf, *end=buf;
char GetChar() {
if (front == end) {
end = buf + fread(front = buf, 1, L, stdin);
if (front == end) return -1;
}
return *(front++);
}
} template <typename T>
inline void qr(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch=IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = (x << 1) + (x << 3) + (ch ^ 48), ch = IPT::GetChar();
if (lst == '-') x = -x;
} template <typename T>
inline void ReadDb(T &x) {
rg char ch = IPT::GetChar(), lst = ' ';
while ((ch > '9') || (ch < '0')) lst = ch, ch = IPT::GetChar();
while ((ch >= '0') && (ch <= '9')) x = x * 10 + (ch ^ 48), ch = IPT::GetChar();
if (ch == '.') {
ch = IPT::GetChar();
double base = 1;
while ((ch >= '0') && (ch <= '9')) x += (ch ^ 48) * ((base *= 0.1)), ch = IPT::GetChar();
}
if (lst == '-') x = -x;
} namespace OPT {
char buf[120];
} template <typename T>
inline void qw(T x, const char aft, const bool pt) {
if (x < 0) {x = -x, putchar('-');}
rg int top=0;
do {OPT::buf[++top] = x % 10 + '0';} while (x /= 10);
while (top) putchar(OPT::buf[top--]);
if (pt) putchar(aft);
} const int maxn = 500010; ll n, k, ans;
char MU[maxn], CU[maxn]; int main() {
freopen("1.in", "r", stdin);
qr(n); qr(k);
do MU[1] = IPT::GetChar(); while ((MU[1] > 'z') || (MU[1] < 'a'));
for (rg int i = 2; i <= n; ++i) MU[i] = IPT::GetChar();
do CU[1] = IPT::GetChar(); while ((CU[1] > 'z') || (CU[1] < 'a'));
for (rg int i = 2; i <= n; ++i) CU[i] = IPT::GetChar();
ll pre = 1;
for (rg int i = 1; i <= n; ++i) {
pre <<= 1;
if (MU[i] == 'b') --pre;
if (CU[i] == 'a') --pre;
if (pre >= k) {
ans += 1ll * k * (n - i + 1);
break;
}
ans += pre;
}
qw(ans, '\n', true);
return 0;
}

【贪心/Trie】【CF1083B】 The Fair Nut and Strings的更多相关文章

  1. [CF1083B]The Fair Nut and Strings

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

  2. CF 1083 B. The Fair Nut and Strings

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

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

  4. CF1083B The Fair Nut and String

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

  5. Codeforces 1083B The Fair Nut and Strings

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

  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. ACM中常见错误提示解析

    Output Limit Exceeded 多数发生在递归遍历的过程中,多输出了一些内容(比如说空格).Output Limit Exceeded还指如果输入某一组数据,你的程序返回的结果是一直输出某 ...

  2. 笔试题——C++开发简单记录错误模块

    题目:链接:https://www.nowcoder.com/questionTerminal/67df1d7889cf4c529576383c2e647c48 来源:牛客网 解析及代码来源:http ...

  3. Mysql数据库的四大特性

    Mysql数据库事务的四大特性(ACID) 事务:把一组密不可分的操作系列集合在一起,这些操作要么全部执行,要么全部不执行. 1.原子性:事务是内定义的操作是一个整体,是不可分割的. 2.一致性:事务 ...

  4. yum安装lnmp

    python其他知识目录 1.安装LNMP之前要安装EPEL,以便安装源以外的软件,如Nginx,phpMyAdmin等. yum install epel-release 提示:EPEL,即Extr ...

  5. AbstractQueuedSynchronizer 原理分析 - 独占/共享模式(转)

    1.简介 AbstractQueuedSynchronizer (抽象队列同步器,以下简称 AQS)出现在 JDK 1.5 中,由大师 Doug Lea 所创作.AQS 是很多同步器的基础框架,比如 ...

  6. dobule运算

    DecimalFormat df = new DecimalFormat("0.00"); double rate = (warnMonNum/totalCustCount)*10 ...

  7. Redux和React-Redux的实现(一):Redux的实现和context

    react使用redux做状态管理,实现多个组件之间的信息共享,解决了父子组件.兄弟组件之间的复杂通信问题.vue有vuex,总之是一种flux的思想.react提供了react-redux这个库,一 ...

  8. Shell脚本 数据清洗

    需要做的任务是将上图类似的格式的文件进行处理,将年月日小时分别提取出来放到每行的行尾(上图已清洗好) 自己的思路是先用cut命令将每行的年月日小时提取出来,分别给一个变量,然后再循环利用sed命令将年 ...

  9. Javascript面向对象二

    Javascript面向对象二 可以通过指定原型属性来对所有的对象指定属性, Object.prototype.name="zhangsan"; Object.prototype. ...

  10. 第一次spring冲刺第5天

    今天进行讨论基础功能的核心代码方面,还有简单的讨论继续关于界面的美化, 计算生成的答案功能 public class Core {// char[]h={'+','-','*','/'};int re ...