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的多少个字符串。然后给出一个最小字符串s,最大字符串t,满足对于所有的k个字符串有s<=S<=t。
最后求满足条件的k个字符串(自己构造)的不同前缀数量的和。
题解:
这题很巧妙,设dp(i)表示长度为i的前缀的数量和,一开始有dp(1)=0。
后来随着长度的增加,我们每次可以在最后加一个a或者b,所以转移方程为dp(i)=2*dp(i-1)。
但是由于有最大最小字符串的限制,当si=b时,dp(i)会多加一个;当ti=a时,dp(i)也会多加一个。减去即可。
可以用归纳法来证明,假设当前循环到第i层,前缀长度i-1满足限制条件。那么si=b时,dp(i-1)中,只有一个后面加上a时(这个长度为i-1的字符串等于字符串s的前i-1位),会小于s;对于ti=a也同理。(考虑共同前缀)
最后输出求和min(dp(i),k)。(k个字符串任意长度的前缀最多也只有k个)
注意一下代码的细节。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+;
ll n,k;
char s[N],t[N];
ll dp[N];
int main(){
cin>>n>>k;
scanf("%s%s",s,t);
dp[]=;
ll ans = ;
for(int i=;i<=n;i++){
char sc = s[i-],tc = t[i-];
dp[i]=2ll*dp[i-];
if(sc=='b') dp[i]--;
if(tc=='a') dp[i]--;
if(dp[i]>=k){
dp[i]=k;
ans+=(n-i)*k;
break ;
}
}
for(int i=;i<=n;i++) if(dp[i]) ans+=dp[i];
printf("%I64d",ans);
return ;
}
还有一种就是把字符串看为二进制的,那么数量就为两个二进制相减。
直接给代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 ;
char s[N],t[N];
ll n,k;
int main(){
cin>>n>>k;
scanf("%s %s",s,t);
ll a=,b=;
ll ans =;
for(int i=;i<n;i++){
ll now;
a<<=;b<<=;
if(s[i]=='b') a++;
if(t[i]=='b') b++;
now = b-a+;
if(now>=k){
ans+=(n-i)*k;
break ;
}
ans+=now;
}
printf("%I64d",ans);
return ;
}
Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings的更多相关文章
- 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 题意: 给出一棵树,走不重复的 ...
- Codeforces Round #526 (Div. 2) C. The Fair Nut and String
C. The Fair Nut and String 题目链接:https://codeforces.com/contest/1084/problem/C 题意: 给出一个字符串,找出都为a的子序列( ...
- Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path 树上dp
D. The Fair Nut and the Best Path 题意:给出一张图 点有权值 边也要权值 从任意点出发到任意点结束 到每个点的时候都可以获得每个点的权值,而从边走的时候都要消耗改边的 ...
- Codeforces Round #526 (Div. 2) Solution
A. The Fair Nut and Elevator Solved. 签. #include <bits/stdc++.h> using namespace std; #define ...
- Codeforces Round #526 (Div. 1)
毕竟是上紫之后的第一场div1,还是太菜了啊,看来我要滚回去打div2了. A. The Fair Nut and the Best Path 这题本来是傻逼贪心dfs,结果我越写越麻烦,然后就只有1 ...
- A. The Fair Nut and Elevator (Codeforces Round #526 (Div. 2))
A. The Fair Nut and Elevator 好笨啊QAQ. 暴力枚举的题,连分类都不用. 从电梯初始位置到第一层.人到第一层.间隔的层数,往返路程. #include <bits/ ...
- Codeforces Round #526 (Div. 2) A.B
A. The Fair Nut and Elevator 题目链接:https://codeforces.com/contest/1084/problem/A 题意: 一栋房子有n层楼,同时有个电梯( ...
- Codeforces Round #449 (Div. 2)-897A.Scarborough Fair(字符替换水题) 897B.Chtholly's request(处理前一半) 897C.Nephren gives a riddle(递归)
A. Scarborough Fair time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [Codeforces Round #526 (Div. 2)]
https://codeforces.com/contest/1084 A题 数据量很小,枚举就行 #include<iostream> #include<cstdio> #i ...
随机推荐
- Moodle 3.4中添加小组、大组、群
Moodle在高中应用时经常要用到年级.班级和小组,我们可以用群.大组.小组来代替. 小组设置:网站首页-->现有课程-->右上角的设置按钮-->更多-->用户-->小组 ...
- Python: 列表的两种遍历方法
方法一:以列表中元素的下标进行访问 def traverse1(list1): for i in range(len(list1)): print(list1[i], end=' ') print() ...
- sql插入查询出的数据,主键递增
INSERT INTO C_DPRECORD SELECT (SEQ_C_DPRECORD.NEXTVAL ) AS ID, DEV_ID, DEV_CHNNUM, DEV_NAME, DEV_CHN ...
- jmeter上传视频图片附件
http上传附件一般用的Content-Type: multipart/form-data;文中是先通过fiddler抓取手机端的请求,然后通过jmeter模拟该请求,如果有接口文档,则可以跳过抓包这 ...
- BeyondCompare3 提示许可证密钥已被撤销解决方法
今天对比文件提示 许可证密钥已被撤销:3281-0350! 找过了几个注册码还是不行. 正确简单的解决方法: 1.找到 BCState.xml 文件 2.编辑器打开,删除<TCheckFor ...
- ZooKeeper的伪分布式集群搭建
ZooKeeper集群的一些基本概念 zookeeper集群搭建: zk集群,主从节点,心跳机制(选举模式) 配置数据文件 myid 1/2/3 对应 server.1/2/3 通过 zkCli.sh ...
- CUDA9.0+tensorflow-gpu1.8.0+Python2.7服务器环境搭建经验
最近在实验室的服务器上搭建Tensorflow,CUDA是默认的9.0,管理员已经装好了,同时环境变量都已经配好. 直接用Anaconda创建的虚拟环境,使用pip install tensorflo ...
- 从零开始搭建一个react项目
Nav logo 120 发现 关注 消息 4 搜索 从零开始搭建一个react项目 96 瘦人假噜噜 2017.04.23 23:29* 字数 6330 阅读 32892评论 31喜欢 36 项目地 ...
- kvm-1
yum install libvirt* virt-* qemu-kvm* -y systemctl start libvirtd.service systemctl status libvirtd. ...
- HDU 4714 Tree2cycle(树状DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup)
Description A tree with N nodes and N-1 edges is given. To connect or disconnect one edge, we need 1 ...