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. PHP has encountered an Access Violation at 01F4A622解决方法

    php搭建的网站出现以下问题的解决方法分享: Z-blog,DedeCMS,Dsicuz!,PhpWind,PhpCMS,帝国CMS等都有可能出现php访问冲突问题. 今天访问网站发现出现了一个错误& ...

  2. Tomcat安全管理规范

    s 前言 随着公司内部使用Tomcat作为web应用服务器的规模越来越大,为保证Tomcat的配置安全,防止信息泄露,恶性攻击以及配置的安全规范,特制定此Tomcat安全配置规范. 定位:仅对tomc ...

  3. [mysql] 归档工具pt-archiver,binlog格式由mixed变成row

    pt-archiver官方地址:https://www.percona.com/doc/percona-toolkit/3.0/pt-archiver.html 介绍:归档数据,比如将一年前的数据备份 ...

  4. 团队项目之开题scrum meeting

    scrum meeting 会议记 一.会议要点: 1.确定成员角色: 2.讨论关于项目的疑问: 3.制定一周内成员任务. 二.具体会议内容: 1.成员角色: PM:杨伊 Dev:徐钧鸿 刘浩然 张艺 ...

  5. 《TCP/IP 详解 卷1:协议》第 4 章:地址解析协议

    链路层是经过单一链路通信的协议层. IP 网络层协议的设计目标是为跨越不同物理类型的.多节点网络的 packet ,提供主机寻址.路由操作. 在其中要注意的一点是:网络层使用的地址和底层网络硬件使用的 ...

  6. 团队作业7——第二次项目冲刺(Beta版本12.09——12.10)

    1.当天站立式会议照片 本次会议在5号公寓3楼召开,本次会议内容:①:熟悉每个人想做的模块.②:根据项目要求还没做的完成. 2.每个人的工作 经过会议讨论后确定了每个人的分工 组员 任务 陈福鹏 倒计 ...

  7. 04_Java基础语法_第4天(数组)_讲义

    今日内容介绍 1.流程控制语句switch 2.数组 3.随机点名器案例 01switch语句解构 * A:switch语句解构 * a:switch只能针对某个表达式的值作出判断,从而决定程序执行哪 ...

  8. Mongodb 分片操作实战

    由于生产环境中一般使用zoomkeeper做config节点的仲裁节点,zoomkeeper会在三个config节点中挑选出一台作为主config节点.且mongos节点一般是两个节点,必须做高可用, ...

  9. intel 的架构图

    第一代: 第二代 第三代 以及对比

  10. C++ 查看预处理后的源文件(查看真实代码)

    gcc -E filename.cpp 会生成 filename.cpp 的预处理文件,这样就能看到宏展开后的代码,用于理解和调试宏非常有帮助.   http://www.qtdebug.com/cp ...