题意

给出两个长度为n的01字符串S和T。

选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀。

这是一道思路比较奇怪的类似计数dp的题。

首先考虑如果把选出的这些串插入到一个trie树中的话,算产生的贡献可以理解为,从根节点向下画了k条长度为n的线,最大化它们所经过的点的总个数。

进一步发现,每一层节点如果不受s和t的限制的话,一定可以满足每一层都有k个点被经过。

因此,剩下要做的就是算一下这个s和t的限制,即每一层有多少个节点。

记dp[i][0/1][0/1]表示第i层,满足前i位是否与s相同,前i位是否与t相同,按照定义转移即可,复杂度O(n)。

最后统计答案的时候把每一层的节点个数和k取min后加入答案即可。

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 1100000
#define L 1000000
#define eps 1e-7
#define ll long long
using namespace std;
inline ll read()
{
char ch=0;
ll x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
const ll inf=1e14+7;
char s[N],t[N];
ll dp[N][2][2];
int main()
{
ll n=read(),m=read();
scanf("%s",s+1);scanf("%s",t+1);
dp[0][1][1]=1;
for(ll i=0;i<n;i++)
{
if(s[i+1]==t[i+1])dp[i+1][1][1]=dp[i][1][1];
else dp[i+1][1][0]=dp[i+1][0][1]=dp[i][1][1];
dp[i+1][1][0]+=dp[i][1][0];
dp[i+1][0][1]+=dp[i][0][1];
dp[i+1][0][0]=min(2*dp[i][0][0],inf); if(s[i+1]=='a')
dp[i+1][0][0]=min(dp[i+1][0][0]+dp[i][1][0],inf);
if(t[i+1]=='b')
dp[i+1][0][0]=min(dp[i+1][0][0]+dp[i][0][1],inf);
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
ll tot=0;
for(ll j=0;j<=1;j++)
for(ll k=0;k<=1;k++)
tot=min(tot+dp[i][j][k],m);
ans+=tot;
}
printf("%lld",ans);
return 0;
}

CF1083B The Fair Nut and String的更多相关文章

  1. 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的子序列( ...

  2. C. The Fair Nut and String 递推分段形dp

    C. The Fair Nut and String 递推分段形dp 题意 给出一个字符串选择一个序列\({p_1,p_2...p_k}\)使得 对于任意一个\(p_i\) , \(s[p_i]==a ...

  3. [CF1083B]The Fair Nut and Strings

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

  4. Codeforces Round #526 C - The Fair Nut and String /// 组合递推

    题目大意: 给定原字符序列 找出其中所有子序列满足 1.序列内字符都为a 2.若有两个以上的字符 则相邻两个字符在原序列中两者之间存在字符b 的数量 将整个字符序列用b分开 此时再得到每个b之间a的数 ...

  5. CF1083E The Fair Nut and Rectangles

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

  6. CF 1083 B. The Fair Nut and Strings

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

  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) E. The Fair Nut and Strings

    E. The Fair Nut and Strings 题目链接:https://codeforces.com/contest/1084/problem/E 题意: 输入n,k,k代表一共有长度为n的 ...

随机推荐

  1. (二)github的价值意义篇

    为什么需要社会化编程? 如果您是程序员面试官,两者之间你会选择哪一位呢? 能查看以前所写代码的程序员 or 无法查看的程序员 精通最新软件的程序员 or 不精通的程序员 对语言或软件差异带来的不同文化 ...

  2. Unable to load the Wrapper's native library because none of the following files及解决方法

    在有几个应用中,在启动的时候发现下列警告: The version of the script (3.5.29) doesn't match the version of this Wrapper ( ...

  3. 《网络攻防》实验五:MSF基础应用

    IE浏览器渗透攻击--MS11050安全漏洞 实验准备 1.两台虚拟机,其中一台为kali,一台为Windows Xp Professional(两台虚拟机可以相互间ping通). 2.亚军同学的指导 ...

  4. stm32时钟树讲解

    1.管理好时钟,功耗才能更低

  5. linux下kermit工具的使用

    1.环境: ubuntu16.04 2.背景: 想更换下位机内核 3.使用kermit进行串口传输 举例:传输文件到下位机 2.1首先进入下位机的uboot 2.2 使用uboot自带的命令从串口接收 ...

  6. ZooKeeper 增加Observer部署模式提高性能

    Observer:在不伤害写性能的情况下扩展ZooKeeper. 虽然通过Client直接连接到ZooKeeper集群的性能已经很好了,可是这样的架构假设要承受超大规模的Client,就必须添加Zoo ...

  7. mysql链接 显示 error: 'Access denied for user 'root'@'localhost' (using password: NO)'

    解决方案 https://stackoverflow.com/questions/2995054/access-denied-for-user-rootlocalhost-using-password ...

  8. 【Android实验】组件通信Intent

    实验目的 [TOC] 了解使用Intent进行组件通信原理 掌握使用Intent启动Activity的方法 熟悉和掌握Android组件间通信的方式和技巧 实验要求 设计一个主Activity和一个子 ...

  9. UVa 11039 设计建筑物

    https://vjudge.net/problem/UVA-11039 题意: 有n个绝对值各不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替且绝对值递增. 思路:正数存一个数组,负数 ...

  10. python 获取格式化时间

    #!/usr/bin/python # -*- coding: UTF- -*- import time localtime = time.asctime( time.localtime(time.t ...