B. The Fair Nut and Strings

题目链接

题意:

  在给定的字符串a和字符串b中找到最多k个字符串,使得不同的前缀字符串的数量最多。

分析:

   建出trie树,给定的两个字符串就是trie树上的两条长度为n路径,那么就是在第n层的所有节点中,找到不大于k个点,(第n层的每个点向上到根的路径的路径上的字符组成一个长度为n字符串)。

  两个第n层的节点一共会构成2n-lca个不同的前缀。所有可以根据这个贪心的选。每次尽量选已经走过的路径尽量少的路径。 

  从n往后枚举,计算长度为答案为i的可以选几个。

  注意判断一下最后是否可以选择k个, 和已经选了的路径。

代码:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define fi(s) freopen(s,"r",stdin)
#define fo(s) freopen(s,"w",stdout)
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int N = ;
char a[N], b[N]; int main () {
int n = read(); LL k; cin >> k;
scanf("%s%s", a + , b + ); LL now = , ans = , tot = ; for (int i = ; i <= n; ++i) {
now = now * ;
if (a[i] == 'b') now --;
if (b[i] == 'a') now --;
if (now >= k) { tot = k; break; }
}
if (tot == ) tot = now; now = ;LL last = ;
for (int i = ; i <= n; ++i) {
now = 1ll * now * ;
if (a[i] == 'b') now --;
if (b[i] == 'a') now --;
LL t = min(k, now); t = min(t, tot); t = min(t, now - last); t = max(t, 0ll);
ans += 1ll * t * (n - i + );
k -= t; tot -= t; last += t;
if ((!k) || (!tot)) break;
}
cout << ans;
return ;
}

CF 1083 B. The Fair Nut and Strings的更多相关文章

  1. 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 题意: 在一棵树内找一条路径,使得从起点 ...

  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. CodeForces 1083 E The Fair Nut and Rectangles 斜率优化DP

    The Fair Nut and Rectangles 题意:有n个矩形,然后你可以选择k个矩形,选择一个矩形需要支付代价 ai, 问 总面积- 总支付代价 最大能是多少, 保证没有矩形套矩形. 题解 ...

  4. Codeforces 1083B The Fair Nut and Strings

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

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

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

  6. [CF1083B]The Fair Nut and Strings

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

  7. CF1083E The Fair Nut and Rectangles

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

  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. 协议森林08 不放弃 (TCP协议与流通信)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! TCP(Transportation Control Protocol)协议与IP ...

  2. flume MemoryChannel 源代码解析

    1.先分析三个LinkedBlockingDeque<Event>类型的takeList,putList,queue putList:  存放的是来自source生产的数据,通过调用doP ...

  3. 如何使用Loadrunner12录制WebSocket脚本

    简单说一下,关于Loadrunner12对WebSocket的支持可以参照以下文档: http://community.hpe.com/t5/LoadRunner-and-Performance/As ...

  4. Kali-linux破解LM Hashes密码

    LM(LAN Manager)Hash是Windows操作系统最早使用的密码哈希算法之一.在Windows 2000.XP.Vista和Windows 7中使用了更先进的NTLMv2之前,这是唯一可用 ...

  5. deque详解

    deque是double-ended queue的简称,deque和vector几乎上是一样的,使用的非常少,定义在<deque>头文件里: deque和vector的区别在于: 1)de ...

  6. 配置mysql允许远程链接

    默认情况下,mysql帐号不允许从远程登陆,只能在localhost登录.本文提供了二种方法设置mysql可以通过远程主机进行连接. 修改用户表的数据 登入mysql后,更改 mysql 数据库里的 ...

  7. 页面QQ在线咨询、在线交谈代码

    页面QQ在线咨询.在线交谈代码 样式一: <a target="blank" rel="nofollow" href="tencent://me ...

  8. [LuoguP4711]分子质量(小模拟+玛丽题)

    --这个题我居然可以把他\(1A\)--真是让我不知其可\(qwq\) \(Link\) \(emmmm\)好像发现了什么固定的套路(?)-- 大概就是这种题总会有 1.读入数 方案:快读即可. 2. ...

  9. 《Android应用测试与调试实战》读书笔记

    一 本书概述 自动化测试篇:Android应用可以使用Java语言配合SDK,也可以使用HTML5技术,还可以用C/C++语言配合NDK技术编写,本书中涵盖了针对这三种技术编写的应用所采用的测试技术. ...

  10. Gradle Goodness: Renaming Files while Copying

    With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...