CF 1083 B. The Fair Nut and Strings
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的更多相关文章
- 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 题意: 在一棵树内找一条路径,使得从起点 ...
- 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的 ...
- CodeForces 1083 E The Fair Nut and Rectangles 斜率优化DP
The Fair Nut and Rectangles 题意:有n个矩形,然后你可以选择k个矩形,选择一个矩形需要支付代价 ai, 问 总面积- 总支付代价 最大能是多少, 保证没有矩形套矩形. 题解 ...
- Codeforces 1083B The Fair Nut and Strings
Description 给定两个由 \('a'\), \('b'\) 组成的字符串 \(a\), \(b\),以及两个整数 \(n\) 和 \(k\) \(n\) 表示字符串 \(a\),\(b\) ...
- 【贪心/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 ...
- CF1083E The Fair Nut and Rectangles
CF1083E The Fair Nut and Rectangles 给定 \(n\) 个平面直角坐标系中左下角为坐标原点,右上角为 \((x_i,\ y_i)\) 的互不包含的矩形,每一个矩形拥有 ...
- CF1083A The Fair Nut and the Best Path
CF1083A The Fair Nut and the Best Path 先把边权搞成点权(其实也可以不用),那么就是询问树上路径的最大权值. 任意时刻权值非负的限制可以不用管,因为若走路径 \( ...
- 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 题意: 给出一棵树,走不重复的 ...
随机推荐
- JavaScript的事件对象_滚轮事件
用户在使用键盘时会触发键盘事件.“DOM2 级事件”最初规定了键盘事件,结果又删除了相应的内容.最终还是使用最初的键盘事件,不过 IE9 已经率先支持“DOM3”级键盘事件. 一.键码 在发生 key ...
- POJ3347 Kadj Squares
嘟嘟嘟 题意:给出一堆正方形的边长,且这些正方形都是\(45 ^ {\circ}\)斜放着并且紧挨着的,求从上往下看能看到几个正方形. 真是一道好题--跟计算几何关系不大. 想一下,如果我们能求出正方 ...
- oracle数据库之用户管理
转载 Oracle创建用户.角色.授权.建表 一.oracle数据库的权限系统分为系统权限与对象权限: 系统权限( database system privilege )可以让用户执行特定的命令集 ...
- Apache PDFbox开发指南之PDF文档读取
转载请注明来源:http://blog.csdn.net/loongshawn/article/details/51542309 相关文章: <Apache PDFbox开发指南之PDF文本内容 ...
- Infiniband基本知识
InfiniBand架构是一种支持多并发链接的“转换线缆”技术,在这种技术中,每种链接都可以达到2.5 Gbps的运行速度.这种架构在一个链接的时候速度是500 MB/秒,四个链接的时候速度是2 GB ...
- POJ 2524 独一无二的宗教(裸并查集)
题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K ...
- 面试 : C语言 功底 被 鄙视了
第一道:被鄙视 的 C语言语法问题 请看 下面 程序 : 错误程序: void GetMemory( char *p ){ p = (char *) malloc( 100 );}void Test( ...
- GoBelieve service部署常见问题总结
问题1: 大家好,我按照文档的步骤编译im时(make install),出现 can't load package: package main: app_route.go:1:1: expected ...
- ListView的优化问题
listview算是我们app中较为常用的控件之一了.而如何优化也是一个问题. listview的优化一般分为两类. 一布局优化. 对布局的优化是大家了解的. 1.复用convertview.andr ...
- 转发: Angular装饰器
Angular中的装饰器是一个函数,它将元数据添加到类.类成员(属性.方法)和函数参数. 用法:要想应用装饰器,把它放在被装饰对象的上面或左边. Angular使用自己的一套装饰器来实现应用程序各部件 ...