[CC-BSTRLCP]Count Binary Strings
[CC-BSTRLCP]Count Binary Strings
题目大意:
对于一个长度为\(n\)的\(\texttt0/\texttt1\)串\(S\),如果存在一个切分\(i\),使得\(S_{[1,i]}\)与\(S_{[i+1,n]}\)的LCP长度\(>k\),那么称\(i\)是\(S\)的精准切分。
如果\(S\)至少有\(m\)个精准切分,那么称\(S\)是一个切分串。
给定\(n,k,m\),求有多少长度为\(n\)的切分串。
- \(1\le T\le 5\)
- \(1\le n\le50\)
- \(0\le m\le n-1\)
- \(0\le k\le \min(10,n)\)
思路:
枚举前\(k\)位的状态\(s\),\(f[i][j][k]\)表示考虑到第\(i\)位,已经有\(j\)个精准切分,最后匹配的长度为\(k\)的方案数。
预处理每种后缀能匹配\(s\)的多长的前缀,转移时枚举最后加上\(0\)还是\(1\)即可。
时间复杂度\(\mathcal O(4^kk+2^kn^2k)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=51,K=11,M=1024,mod=1e9+7;
int f[2][N][K],g[M][K];
inline void upd(int &a,const int &b) {
(a+=b)%=mod;
}
int main() {
for(register int T=getint();T;T--) {
const int n=getint(),k=getint(),m=getint();
if(k==0) {
printf("%lld\n",(1ll<<n)%mod);
continue;
}
if(m+1==n||k*2>n) {
puts("0");
continue;
}
const int all=(1<<k)-1;
int ans=0;
for(register int s=0;s<=all/2;s++) {
int p[k+1];
for(register int i=0,t=s;i<=k;i++) {
p[k-i]=t;
t>>=1;
}
memset(f[0],0,sizeof f[0]);
for(register int t=0;t<=all;t++) {
for(register int i=1;i<=k;i++) {
int l;
for(l=i;l;l--) {
if(p[l]==(t&((1<<l)-1))) break;
}
g[t][i]=l;
}
f[0][t==s][g[t][k]]++;
}
for(register int i=k*2+1;i<=n;i++) {
const bool cur=i&1;
memset(f[cur],0,sizeof f[cur]);
for(register int j=0;j<=i;j++) {
for(register int i=0;i<=k;i++) {
for(register int b=0;b<2;b++) {
const int t=((p[i]<<1)|b)&all;
const int l=g[t][std::min(i+1,k)];
upd(f[cur][j+(l==k)][l],f[!cur][j][i]);
}
}
}
}
for(register int j=m+1;j<=n;j++) {
for(register int i=0;i<=k;i++) {
(ans+=f[n&1][j][i])%=mod;
}
}
}
printf("%d\n",(ans<<1)%mod);
}
return 0;
}
[CC-BSTRLCP]Count Binary Strings的更多相关文章
- 【Leetcode_easy】696. Count Binary Substrings
problem 696. Count Binary Substrings 题意:具有相同个数的1和0的连续子串的数目: solution1:还不是特别理解... 遍历元数组,如果是第一个数字,那么对应 ...
- A. You Are Given Two Binary Strings…
A. You Are Given Two Binary Strings… You are given two binary strings x and y, which are binary repr ...
- 696. Count Binary Substrings - LeetCode
Question 696. Count Binary Substrings Example1 Input: "00110011" Output: 6 Explanation: Th ...
- [LeetCode] Count Binary Substrings 统计二进制子字符串
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- 696. Count Binary Substrings统计配对的01个数
[抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...
- LeetCode Count Binary Substrings
原题链接在这里:https://leetcode.com/problems/count-binary-substrings/description/ 题目: Give a string s, coun ...
- [Swift]LeetCode696. 计数二进制子串 | Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- LeetCode算法题-Count Binary Substrings(Java实现)
这是悦乐书的第293次更新,第311篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第161题(顺位题号是696).给定一个字符串s,计算具有相同数字0和1的非空且连续子串 ...
- LeetCode 696 Count Binary Substrings 解题报告
题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...
随机推荐
- truncate table时存在外键约束的解决办法
以前在使用truncate命令时遇到表存在外键引用时无法执行命令的情况都是用delete来代替,今天又遇到这个问题,于是在网上搜了一把,可以通过如下方式解决: 1.基本思路:先关闭mysql的外键约束 ...
- phoenix表操作
phoenix表操作 进入命令行,这是sqlline.py 配置到path环境变量的情况下 sqlline.py localhost如果要退出命令行:!q 或者 !quit 3.4.1 创建表 ...
- 定时获取MySQL库的大小
定时获取MySQL库的大小 获取数据库单个库的大小命令 [root@admin ~]# cat db_size.txt mysql -h 192.8.1.1 -uUSER -pPASSWORD -e' ...
- Nginx安装配置&反向代理
使用Nginx作为前端服务能够更快更及时的响应静态页面.js.图片等,当客户端请求访问动态页面时由Nginx的反向代理给Apache处理,Apache处理完再交予Nginx返回给客户端. Nginx更 ...
- 通过expdp和impdp将Oracle11g数据导入到Oracle10g中
1 导出过程 1.1 查看目录: select * from dba_directories; 1.2 将目录的操作权限赋值给指定的用户(不执行次步骤可能会出现权限问题): grant read,wr ...
- VMware搭建虚拟机服务器
一.需求点描述: 1.在有路由器的情况下,能够通过固定的外网IP访问路由器中某台实体机中运行的虚拟机. 2.能够通过外网IP访问该虚拟机中的ftp.远程连接.iis.tomcat等. 二.原理分析: ...
- 个人笔记本安装多个jdk(jdk1.7,jdk1.8,jdk1.9,jdk10.0)出现的问题
1.个人笔记本已经安装jdk1.7,jdk1.8,(之前没有在意这个问题).最近想学习jdk10.0,安装以后,环境变量变成了jdk10.0,就是cmd输入命令java -version,显示版本是j ...
- Java 集合Collection与List的详解
1.什么是集合 存储对象的容器,面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式. 集合的出现就是为了持有对象.集合中可以存储任意类型的 ...
- WebApi参数传递实例
Get 1.基础数据类型 1.1方法只含有一个形参 (1)Get传值的本质是通过url字符串拼接(2)Get传递参数本质是url字符串拼接,Request-Head头部传递,Request-Body中 ...
- 山寨版 WP8.1 Cortana 启动 PC
8.1 dev preview 发布以来 Cortana 很受关注 前一段看到有视频演示用 Cortana 来启动 PC 看视频也是启动第三方应用实现的,简单来弄其实就是个语音启动应用 + 网络唤醒么 ...