在给定约束下可以使用a,b和c形成的字符串数
Problem statement:
问题陈述:
Given a length n, count the number of strings of length n that can be made using 'a', 'b' and 'c' with at-most one 'b' and two 'c's allowed.
给定长度n ,计算可以使用'a' , 'b'和'c'且长度最多为'b'和两个'c'的长度为n的字符串的数量。
Example:
例:
Input:
n=1
Output:
3
Possible strings are:
"a", "b", "c"
Input:
n=2
Output:
8
Possible strings are:
"aa", "ab", "ac", "ba", "ca", "bc", "cb", "cc"
Solution:
解:
String alphabets are only {a, b, c}
字符串字母仅为{a,b,c}
Length of string is n. (n>0)
字符串的长度是n。 (n> 0)
Let's consider what can be the possible cases
让我们考虑一下可能的情况
String is only built with 'a', i.e., n 'a' forms the string.
字符串仅使用'a'构建,即n'a '构成字符串。
Count of such string is: 1
该字符串的计数为:1
String built with one 'b' & n-1 'a'
串建有一个“B”及n-1个 “A”
Count of such string is:
该字符串的计数为:
(n/1)=n
(n / 1)= n
One
之一
'b' can be placed at any of n positions, that's why n number of such strings
'b'可以放置在n个位置中的任何位置,这就是为什么n个这样的字符串
String built with one 'b', one 'c' and (n-2) 'a'
用一个'b' ,一个'c'和(n-2) 'a'构建的字符串
Count of such string
这样的字符串数
(n/2)*2=n*(n-1)
(n / 2)* 2 = n *(n-1)
One
之一
'b' and one 'c' can take any of two places out of n and any of 'b' & 'c' can comes first.
'b'和一个'c'可以从n中占据两个位置中的任何一个,并且'b'和'c'中的任何一个都可以排在第一位。
String built with one 'b', two 'c' and (n-3) 'a'
用一个'b' ,两个'c'和(n-3) 'a'构建的字符串
Count of such string
这样的字符串数
(n/3)*3=n*(n-1)*(n-2)/2
(n / 3)* 3 = n *(n-1)*(n-2)/ 2
One
之一
'b' and two 'c' can take any of three places out of n and there are 3 combinations possible between one 'b' & two 'c'.
“b”和2“c”的可以采取任何的三个地方出n和有3种组合之一“B”及2“C”之间的可能。
String built with two 'c' and (n-2) 'a'
用两个'c'和(n-2) 'a'构建的字符串
Count of such string
这样的字符串数
(n/2)=n*(n-1)/2
(n / 2)= n *(n-1)/ 2
Two
二
'c' can take any two of n places.
'c'可以取代n位中的任何两个。
String built with one 'c' and (n-1) 'a'
用一个'c'和(n-1) 'a'构建的字符串
Count of such string
这样的字符串数
(n/1)=n
(n / 1)= n
One
之一
'c' can take any of one places out of n.
'c'可以取n中的任何一位。
Example with explanation
带说明的例子
Let n=2
Case 1: String is only built with 'a', i.e., n 'a' forms the string
"aa"
Total under this category: 1
Case 2: String built with one 'b' & n-1 'a'
"ab"
"ba"
Total under this category: 2//(n)
Case 3: String built with one 'b', one 'c' and (n-2) 'a'
"bc"
"cb"
Total under this category: 2//(n*(n-1))
Case 4: String built with one 'b', two 'c' and (n-3) 'a'
No string in this category
Total under this category: 0
Case 5: String built with two 'c' and (n-2) 'a'
"cc"
Total under this category: 1//(n*(n-1)/2)
Case 6: String built with one 'c' and (n-1) 'a'
"ac"
"ca"
Total under this category: 2//(n)
Total no of strings possible: 1+2+2+0+1+2=8
C++ implementation
C ++实现
#include <bits/stdc++.h>
using namespace std;
int find(int n){//total no of string possible(for details check solution part)
return 1+(n)+n*(n-1)+n*(n-1)*(n-2)/2+n*(n-1)/2+n;
}
int main()
{int n;
cout<<"enter length of string\n";
cin>>n;
cout<<"Number of string possible under ";
cout<<"constraints is : "<<find(n)<<endl;
return 0;
}
Output
输出量
First run:
enter length of string
2
Number of string possible under constraints is : 8
Second run:
enter length of string
4
Number of string possible under constraints is : 39
翻译自: https://www.includehelp.com/icp/count-of-strings-that-can-be-formed-using-a-b-and-c-under-given-constraints.aspx
在给定约束下可以使用a,b和c形成的字符串数相关推荐
- 投资绩效约束下的有限套利(Shleifer,Vishny)
投资绩效约束下的有限套利(Shleifer,Vishny) – 潘登同学的Quant笔记 文章目录 投资绩效约束下的有限套利(Shleifer,Vishny) -- 潘登同学的Quant笔记 行为金融 ...
- 《复杂约束下的多目标优化算法》阅读笔记
摘要:约束多目标优化问题(CMOP)由于需要同时考虑目标和约束,特别是当约束极其复杂时,处理起来比较困难.最近的一些算法在处理具有简单可行域的CMOP时工作得很好,然而,对于具有复杂可行域的CMOP, ...
- 非香农类信息不等式_ICLR2020|北大图灵班本科生满分论文:计算约束下有用信息的信息论...
机器之心转载 来源: 北京大学前沿计算研究中心 作者:许逸伦 本文是第八届国际表征学习会议 (ICLR 2020) 入选口头展示论文 (oral)<基于计算约束下的有用信息的信息论 (A The ...
- 【控制】《多智能体系统的协同群集运动控制》陈杰老师-第9章-多任务约束下多智能体协同编队控制
第8章 回到目录 第10章 第9章-多任务约束下多智能体协同编队控制 9.1 研究背景 9.2 问题描述 系统模型 (9.1) 9.3 多任务约束协调与求解 9.4 多任务切换与编队控制器设计 控制协 ...
- 【量化交易】组合优化三部曲:换手率和alpha模型换手约束下的最优模型时变IC下的多空/多头最优组合换手率
前言 单因子模型,考虑策略风险(即IC时序波动),最大化风险调整后收益的主动增强组合优化 01 无约束下,多空最优组合的换手率的解析解 02 跟踪误差约束下,多头最优组合的换手率的数值优化 03 跟踪 ...
- 等式约束和不等式约束下的KKT条件求法
一.写在前面 本篇内容主要写非线性规划等式约束和不等式约束下的KKT条件,主要通过举例说明. 二.等式约束下的KKT条件 1. 题目描述 考虑等式约束的最小二乘问题 minimizexTxsubjec ...
- linux可以使用的远程管理,linux下可以使用以下()方法进行远程管理
linux下可以使用以下()方法进行远程管理 更多相关问题 1013的倒数是______:______和14互为倒数. 12的倒数是______,______的倒数为47. 试说明按年度计划分配率分配 ...
- 发射瞬时速度约束下的弹道导弹轨迹仿真算法
目录 一.导弹弹道仿真的主要研究思路分析 二.STK中的导弹弹道仿真技术 三.算法描述 四.惯性系下半长轴和偏心率计算模型 五.轨道其他参数的确定 六.仿真结果 参考文献 弹道建模与仿真一般分为4个层 ...
- 6软硬约束下的轨迹优化
Introduction Minimum snap trajectory optimization Minimum snap这种方法只限制轨迹应该会通过的中间路径点(中继点),collision ch ...
最新文章
- python编程入门指南怎么样-如果想学python怎么入门?
- RabbitMQ配置Mqtt协议
- 深度讲解:同步/异步/阻塞/非阻塞/BIO/NIO/apr
- 顶级极客技术挑战赛,你敢来挑战吗?| 大神登峰造极
- 一串最简单的JavaScript代码,在Chrome开发者工具调试器里触发VM8标签的出现
- JSP校园自行车租赁网站平台管理系统
- 基于STM32单片机设计的红外测温仪(带人脸检测)
- ewb交通灯报告和文件_简易交通灯控制逻辑电路设计报告
- php ui设计_什么是ui设计
- Python socket和前端html
- 火山视窗读写ini文件,写配置项与读配置项使用
- 文件隐藏的几种简单方法
- 飞思卡尔普通看门狗与窗口看门狗
- 大学计算机软件破解版,大学理工科学计算器
- 音频文件 数据库存储_刚哥谈架构 (六) 谈谈数据库架构
- .Net Reflector反编译代码与源代码的区别
- 大众mpv_大众mpv商务车7座车型 大众mpv都有哪些?
- utf-8内繁简体转换
- 欢迎随时回家——python制作一个火车票抢票版本(附完整代码,仅供学习参考)
- 犀牛书第2章 JavaScript词法结构