[AGC024F] Simple Subsequence Problem
Problem Statement
You are given a set $S$ of strings consisting of 0 and 1, and an integer $K$.
Find the longest string that is a subsequence of $K$ or more different strings in $S$.
If there are multiple strings that satisfy this condition, find the lexicographically smallest such string.
Here, $S$ is given in the format below:
- The data directly given to you is an integer $N$, and $N+1$ strings $X_0,X_1,...,X_N$. For every $i$ $(0\leq i\leq N)$, the length of $X_i$ is $2^i$.
- For every pair of two integers $(i,j)$ $(0\leq i\leq N,0\leq j\leq 2^i-1)$, the $j$-th character of $X_i$ is
1if and only if the binary representation of $j$ with $i$ digits (possibly with leading zeros) belongs to $S$. Here, the first and last characters in $X_i$ are called the $0$-th and $(2^i-1)$-th characters, respectively. - $S$ does not contain a string with length $N+1$ or more.
Here, a string $A$ is a subsequence of another string $B$ when there exists a sequence of integers $t_1 < ... < t_{|A|}$ such that, for every $i$ $(1\leq i\leq |A|)$, the $i$-th character of $A$ and the $t_i$-th character of $B$ is equal.
Constraints
- $0 \leq N \leq 20$
- $X_i(0\leq i\leq N)$ is a string of length $2^i$ consisting of
0and1. - $1 \leq K \leq |S|$
- $K$ is an integer.
神奇 dp。
记的时候如何区分 1 和 01?统一在前面
考虑暴力怎么做。枚举一个子串,然后数有多少个子串包含这个子串。复杂度 \(4^N\)。
怎么判断一个子串是否是另一个子串的子序列呢?子序列自动机。定义 \(nx_{i,j}\) 为第 \(i\) 位出现的下一个 \(j\) 在哪里,跑就行了。
思考在跑子序列自动机的时候有没有一些重复的状态,发现在某些状态下,已经匹配好的和需要匹配好的串总数是不多的。也就是说,定义 \(dp_{s,t}\) 为已经匹配好的串是 \(s\),等待匹配的串是 \(t\),有多少个大串能转化成这种样子。
发现 \(|S|+|T|\le n\),所以改 dp 定义为 \(dp_{s,i}\) 为串 \(s\),前 \(i\) 个字符为已经匹配好的,剩下的是等待匹配的。
#include<bits/stdc++.h>
using namespace std;
const int N=2.1e6;
int n,k,m,ln[N],ls[N][21][2],dp[21][N],lg[N],s,ans[N],ret;//dp[i][j]表示总串j,匹配了j位的答案,ls[i][j][k]表示数字i在第j位前的第一个k在第几位
char ch;
int main()
{
for(int i=2;i<N;i++)
lg[i]=lg[i>>1]+1;
scanf("%d%d",&n,&k);
for(int i=0;i<=n;i++)
{
for(int j=0;j<(1<<i);j++)
{
ch=getchar();
while(ch<'0'||ch>'9')
ch=getchar();
if(ch^48)
dp[0][1<<i|j]++;
}
}
for(int i=1;i<(1<<n+1);i++)
{
m=lg[i],s=i-(1<<m);
ls[i][0][0]=ls[i][0][1]=-1;
for(int j=1;j<=m;j++)
ls[i][j][0]=ls[i][j-1][0],ls[i][j][1]=ls[i][j-1][1],ls[i][j][s>>(j-1)&1]=j-1;
}
// printf("%d %d\n",ls[6][3][0],ls[6][3][1]);
// printf("%d %d\n",ls[6][2][0],ls[6][2][1]);
// printf("%d %d\n",ls[6][1][0],ls[6][1][1]);
for(int i=0;i<=n;i++)//枚举长度
{
for(int j=(1<<i);j<(1<<n+1);j++)
{
m=lg[j];
int k=j>>(m-i);
ans[k]+=dp[i][j];
if(!dp[i][j])
continue;
if(i==m)
continue;
// printf("hjhyyds:%d %d %d\n",i,j,m);
int p=ls[j][m-i][0],q=ls[j][m-i][1];
// printf("%d %d %d %d %d\n",k,(k<<p+1)|(((1<<p+1)-1&j)),(k<<q+1)|((1<<q+1)-1&j),p,q);
if(~p)
dp[i+1][(k<<p+1)|(((1<<p+1)-1&j))]+=dp[i][j];
if(~q)
dp[i+1][(k<<q+1)|((1<<q+1)-1&j)]+=dp[i][j];
}
}
for(int i=1;i<(1<<n+1);i++)
if(ans[i]>=k)
ret=max(ret,lg[i]);
for(int i=1;i<(1<<n+1);i++)
{
if(ans[i]>=k&&lg[i]==ret)
{
for(int j=lg[i]-1;j>=0;j--)
putchar((i>>j&1)+48);
return 0;
}
}
}
[AGC024F] Simple Subsequence Problem的更多相关文章
- @atcoder - AGC024F@ Simple Subsequence Problem
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定由若干长度 <= N 的 01 字符串组成的集合 S. ...
- [题解] [AGC024F] Simple Subsequence Problem
题目大意 有一个 01 串集合 \(S\),其中每个串的长度都不超过 \(N\),你要求出 \(S\) 中至少是 \(K\) 个串的子序列的最长串,如果有多解,输出字典序最小的那组解. 由于 \(S\ ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- hdu4976 A simple greedy problem. (贪心+DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- bzoj 3489: A simple rmq problem k-d树思想大暴力
3489: A simple rmq problem Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 551 Solved: 170[Submit][ ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
随机推荐
- 文心一言 VS 讯飞星火 VS chatgpt (78)-- 算法导论7.4 2题
二.如果用go语言,证明:在最好情况下,快速排序的运行时间为 Ω(nlgn). 文心一言: 在最好情况下,快速排序的运行时间为Ω(nlgn)的证明如下: 快速排序是一种分治算法,它将一个数组分成两个子 ...
- Spring Boot通过企业邮箱发邮件被Gmail退回的问题解决方法
这两天给我们开发的Chrome插件:Youtube中文配音增加了账户注册和登录功能,其中有一步是邮箱验证,所以这边会在Spring Boot后台给用户的邮箱发个验证信息.如果发邮件,之前的文章教程里就 ...
- 使用 AutoGPTQ 和 transformers 让大语言模型更轻量化
大语言模型在理解和生成人类水平的文字方面所展现出的非凡能力,正在许多领域带来应用上的革新.然而,在消费级硬件上训练和部署大语言模型的需求也变得越来越难以满足. Hugging Face 的核心使命是 ...
- 探索ChatGPT的Fine-tuning和Embeddings
1.概述 今天我们将深入探索ChatGPT的两项核心技术:Fine-tuning(微调)和Embeddings(嵌入).这些技术在现代自然语言处理领域扮演着至关重要的角色,为模型的性能提升和适应特定任 ...
- 在.NET Framework中使用RocketMQ(阿里云版)实战【第二章】
章节 第一章:https://www.cnblogs.com/kimiliucn/p/17662052.html 第二章:https://www.cnblogs.com/kimiliucn/p/176 ...
- CodeForces 1187E Tree Painting
题意:给定一棵\(n\)个点的树 初始全是白点 要求你做\(n\)步操作,每一次选定一个与一个黑点相隔一条边的白点,将它染成黑点,然后获得该白点被染色前所在的白色联通块大小的权值. 第一次操作可以任意 ...
- DevOps |研发效能之环境、程序、配置、SQL变更管理
本文主要是讲如何建立有效的环境.程序.配置.SQL变更和管理平台. 几天前和一个朋友聊到环境.程序的配置变更,SQL变更和整个上线流程.之前我们在这块也做了很多,有做的好的也有做的一般的,借机都总结 ...
- VB快速上手文档教程
前言 本来我想可能不会接触到这个语言, 不过在用excel时需要用到VBA. 这就不得不专门去学习一番. 入了个门, 专门写个文档留着. 万一以后用得到呢- 论VB, 我还是初学者. 如有弄错了的地方 ...
- BS系统的登录鉴权流程演变
1 基础知识 用户登录是使用指定用户名和密码登录到系统,以对用户的私密数据进行访问和操作.在一个有登录鉴权的BS系统中,通常用户访问数据时,后端拦截请求,对用户进行鉴权,以验证用户身份和权限.用户名. ...
- android图片缩放双击旋转效果
需要jar源码的请留言吧. 部分源码 demo下载地址 package uk.co.senab.photoview.sample; import android.app.ListActivity ...