Count number of binary strings without consecutive 1’s

LaTeX新手必看:遇到‘sty文件找不到’别慌,手把手教你用tlmgr和CTAN搞定(以algorithmicx包为例) 本文详细解析了LaTeX编译中常见的'sty文件找不到'错误,以algorithmicx包为例,手把手教你使用tlmgr和CTAN解决宏包缺失问题。从报错诊断到安装方法,再到预防措施,提供了一套完整的解决方案,帮助LaTeX新手快速掌握包管理技巧。 阅读详情

一、题目

1. Count number of binary strings without consecutive 1’s

Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consecutive 1's .
Examples:

  Input:  2 
  Output: 3 // The 3 strings are 00, 01, 10 
  ​
  Input: 3 
  Output: 5 // The 5 strings are 000, 001, 010, 100, 101

二、题意

给定一个正整数n(3≤n≤90),数出长度为n的所有可能的不同二进制串的个数,使得串中没有连续的1出现。

 

三、解题思路及算法

1、回溯+深搜

长度为n的所有二进制串形成一棵子集树,所以可以用DFS遍历子集树,遍历过程中检查是否有连续的1出现,时间复杂度为O(2^n)。

// 深搜回溯法 
#include <iostream>
using namespace std;
long long total = 0;
bool judge(string s){
	int length = s.length();
	int num = 0;
	for(int i=0; i<length-1; i++){
		if(s[i] == '1' && s[i+1] == '1')
			return false;
	}
	return true;
}
void calculate(string s, int index, int n){
	if(index == n) {
		if(judge(s)){total++;} 
		return;
	}
	else{
		calculate(s, index+1, n);
		s[index] = '1';
		calculate(s, index+1, n);
	}
}
int main (){
	int n;
	cin>>n;
	string s="";
	for(int i=0; i<n; i++){
		s+='0';
	}
	calculate(s, 0, n);
	cout<<total<<endl;
	
	return 0;
} 

2、动态规划(DP)
 思路:设a[i-2],a[i-1], a[i]分别为长度为i-2,i-1,i的不含连续1的二进制字符串的个数
 那么从长度为i的来考虑:
 1、若最后一个值为0,则这种情况下不含连续1的二进制字符串个数为a[i-1]
  2、若最后一个值为1,则这种情况下要符合条件那么倒数第二个的值只能为0
   此时符合条件的个数为a[i-2](理由和1相同) 

时间复杂度:O(n)。

#include <iostream>
using namespace std;
int main (){
	int n;
	cin>>n;
	long long a[n+1];   // n最大为90,那么总数的量级是2^90数量级,因此需要大数 
	a[1] = 2; a[2] = 3;
	for(int i=3; i<=n; i++){
		a[i] = a[i-1] + a[i-2];
	}
	cout<<a[n]<<endl;
	
	return 0;
} 

参考博文:https://blog.youkuaiyun.com/climber16/article/details/81604342

2018南京大学计算机夏令营机试 1. Count number of binary strings without consecutive 1’s Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consecutive 1's . Examples... 阅读详情

相关推荐

淘宝扫码登录避坑指南:如何避免异地登录风控并稳定获取Cookie(附Python代码)

本文深入解析了淘宝扫码登录的风控机制,提供了从环境仿真到代码实现的完整避坑指南。通过精细化伪装请求头、管理会话与IP策略,并附上Python示例代码,帮助开发者稳定获取Cookie,有效避免异地登录风控,实现自动化流程的可靠集成。

l6m7n8的博客 590

解决:输入一个n输出以n为长度的二进制数串的所有可能,并从小到大输出所有

一开始看到这个问题时,会想到用0和1的排序,去寻找其中的规律,总结出表达式。 看似可行,但是在寻找规律时会发现每种情况之间都有一定的规律但不好用表达式表示出来,如: n=3; 输出结果为: 000;. 001;. 010;. 011;. 100;. 101;. 110;. 111;. 就可以看到第一列四个一组,有两组;第二列两个一组,四组;第三列,一个一组,八组; 但是发现了规律,要写出关系式...

missli0的博客 4208

九齐NY8A051F低功耗模式全解析:从待机到休眠的省电实战

本文全面解析台湾九齐NY8A051F MCU的低功耗模式,从待机到休眠的省电实战技巧。通过实测数据展示四种工作模式的电流消耗差异,提供模式切换的标准流程和唤醒时序优化策略,并分享外围电路省电设计和低电压检测的实战应用,帮助开发者实现物联网设备的超长续航。

weixin_30839881的博客 408

【回溯法&动态规划】NJU2018

1. 回溯法基本框架 给你输入一个数组 nums 和一个正整数 k,请你判断 nums 是否能够被平分为元素和相同的 k 个子集。 // k 个桶(集合),记录每个桶装的数字之和 int[] bucket = new int[k]; // 穷举 nums 中的每个数字 void backtrack(int[] nums, int index) { // base case if (index == nums.length) { return; } // 穷举

weixin_45209148的博客 232

IKM 测试题 - Java SE 8 评估测试题目

IKM 测试题 - Java SE 8 评估测试题目

licheng11y的博客 1313

5分钟搞定三网话费余额查询:手把手教你用PHP+HTML搭建查询系统(含API调用避坑指南)

本文详细介绍了如何使用PHP+HTML快速搭建三网话费余额查询系统,涵盖API调用避坑指南、前端UI设计及性能优化。通过统一处理移动、电信、联通三大运营商接口差异,实现高可用查询功能,并提供安全防护与生产部署建议,帮助开发者高效构建稳定可靠的话费查询工具。

weixin_29016833的博客 259

leetcode 600 Count number of binary strings without consecutive 1 不出现连续1二进制数量

Given a positive integer N, count all possible distinct binary strings of length N such that there are no consecutive 1’s. Examples: Input: N = 2 Output: 3 // The 3 strings are 00, 01, 10 Input:...

JackZhangNJU的专栏 522

BitLocker磁盘锁定解决方法

摘要:本文介绍了BitLocker加密磁盘被锁的两种情况及解决方法。对于系统盘锁定,建议在微软账户中查找48位恢复密钥;数据盘锁定可尝试密码或恢复密钥解锁。若密钥丢失,可通过IT支持或数据恢复服务尝试解决。预防措施包括妥善备份密钥到微软账户和离线位置,避免随意更改系统硬件配置。BitLocker安全性高,恢复密钥是解锁关键,提醒用户务必做好备份以防数据丢失。(148字)

yueke06442300的专栏 2674

geeksforgeeks Count number of binary strings without consecutive 1’s

转自:http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/ Count number of binary strings without consecutive 1’s Given a positive integer N, count all possible distin

martin_liang的专栏 552

开源ISP实战(1)——基于Zynq7020的Vivado工程移植与调试

本文详细介绍了将开源ISP项目移植到正点原子Zynq7020开发板的实战过程。重点阐述了在Vivado中进行工程创建、IP核导入、Block Design框架搭建、时钟配置与管脚约束修改等关键步骤,并提供了系统联调与常见问题的排查思路,为FPGA图像处理开发者提供了完整的移植与调试指南。

weixin_29191081的博客 200

思科 MIB 库

思科 MIB

m0_54931486的博客 770

2018年南京大学夏令营机试-1

2018年南京大学夏令营机试-1 题目描述 Count number of binary strings without consecutive 1’s 输入 Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consec...

jindayue的博客 730

Vulkan入门系列3 - 绘制三角形代码解析(全)

【代码】Vulkan入门系列3 - 绘制三角形完整代码(带详细注释)

GPU全栈博主 1033

geeksforgeeks Count number of binary strings without consecutive 1′s

题目描述和算法见:http://www.geeksforgeeks.org/count-number-binary-strings-without-consecutive-1s/ 思路是用DP,考虑从

thestoryofsnow的专栏 799

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

2018年南京大学计算机系夏令营上机A

题目描述 Count number of binary strings without consecutive 1’s 输入 Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consecutive 1’s . 题目...

圣托里尼的日落啊~的博客 356

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

爬虫教程( 4 ) --- scrapy-redis、集群、curl-cffi、scrapy-stealth、aio-scrapy、omnicrawl-core、反检测(反爬)浏览器

分布式爬虫 scrapy-redis、集群

freeking101的博客 6822

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【算法】大厂机考总结和HOT100

【最新最全】华为OD历年机试题集合+OJ刷题网址+刷题经验_华为的外包都要提前刷题 数据库刷题的地址是哪个-优快云博客。

qq_58358642的博客 378

【Python CheckiO 题解】Count Consecutive Summers

CheckiO 是面向初学者和高级程序员的编码游戏,使用 Python 和 JavaScript 解决棘手的挑战和有趣的任务,从而提高你的编码技能,传送门:https://checkio.org/,本博客主要记录自己在闯关时的做题思路和实现代码,同时也学习学习其他大神写的代码。 题目描述 【Count Consecutive Summers】:一个正整数可以用几个连续的正整数之和来表示,例...

BOB'S BLOG 1128

LeetCode 之 string字符串一

1. Length of last word Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist,

coder 进阶的专栏 1万+

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should ru...

u013383813的博客 280

485. Max Consecutive Ones

485. Max Consecutive OnesGiven a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits

造梦空间 276

Count the consecutive zero bits (trailing) on the right with multiply and lookup

我在网上看到了一点神奇的代码,用来计算一个数字末尾连续零的个数。 刚好我在优化一个I2C读写函数(只写入I2C特定bit),觉得这个很有用。经过尝试,确实没问题。 下面我隆重介绍一下: Count the consecutive zero bits (trailing) on the right with multiply and lookup unsigned int v; ...

305

leetcode系列485-最大连续1的个数

【题目概要】 485. Max Consecutive Ones Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum numbe

每一天的小白 240

LeetCode#696: Count Binary Substrings

Description Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0’s and 1’s, and all the 0’s and all the 1’s in these substrings are grouped consecutive...

水木今山的博客 367

18年机试(nju)

18年机试(nju) Count number of binary strings without consecutive 1’s Given a positive integer n(3≤n≤90), count all possible distinct binary strings of length n such that there are no consecutive 1’s . ...

Yoc Lu 916
上一篇: Linux服务器上Mysql安装及基本使用命令
下一篇: Missing number
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值