在给定约束下可以使用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

让我们考虑一下可能的情况

  1. String is only built with 'a', i.e., n 'a' forms the string.

    字符串仅使用'a'构建,即n'a '构成字符串。

    Count of such string is: 1

    该字符串的计数为:1

  2. 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个这样的字符串

  3. 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'中的任何一个都可以排在第一位。

  4. 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”之间的可能。

  5. 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位中的任何两个。

  6. 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376918.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Robotlegs轻量级AS3框架

Robotlegs是一个用来开发Flash&#xff0c;Flex和AIR应用的纯AS3微架构(框架)。Robotlegs专注于将应用程序各层排布在一起并提供它们相互通讯的机制。Robotlegs试图通过提供一种解决常见开发问题的经过时间检验的架构解决方案来加速开发。Robotlegs无意锁定你到框架&#xff0c…

Python | 字符串isdecimal(),isdigit(),isnumeric()和Methods之间的区别

The methods isdigit(), isnumeric() and isdecimal() are in-built methods of String in python programming language, which are worked with strings as Unicode objects. These functions return either true or false. 方法isdigit() &#xff0c; isnumeric()和isdecim…

mssql2000 数据库一致性错误修复

一般情况下&#xff0c;引起分配错误的原因是磁盘损坏或突然停电&#xff1b;一致性错误可能是数据库中的表或索引坏&#xff0c;一般都可修复。1、查看红色字体&#xff0c;并把有错误的数据库表名记录下来&#xff0c;或把索引损坏的表名记录下来。2、把数据库设置为单用户模…

Linux系统上的程序调优思路概要

目录文件系统Linux内核应用程序架构设计性能监控性能测试CPU内存网络磁盘IO文件系统 Linux内核 应用程序 架构设计 性能监控 性能测试 CPU 内存 网络 磁盘IO

bzoj1699[Usaco2007 Jan]Balanced Lineup排队

Description 每天,农夫 John 的N(1 < N < 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 < Q < 180,000) 个可能的牛的…

mcq 队列_基于人工智能的智能体能力倾向问答(MCQ) 套装1

mcq 队列1) Which of the following are the main tasks of an AI agent? Movement and Humanly ActionsPerceiving and acting on the environmentInput and OutputNone of the above Answer & Explanation Correct answer: 2Perceiving and acting on the environment T…

CentOS 服务器搭建及排查注意事项

时间 时区&#xff1a; /usr/sbin/ntpdate cn.pool.ntp.org && /sbin/hwclock yum install ntp -y /usr/sbin/ntpdate cn.pool.ntp.org && /sbin/hwclock 检查 /etc/php.ini cgi.fix_pathinfo0检查磁盘是否满了 df -h 如果PHP 无法种cookie&#xff0c;检查 P…

单例模式的七种实现方法(java版)

代码参考&#xff1a;《重学Java设计模式小傅哥》 目录1、静态类使用2、懒汉模式&#xff08;线程不安全&#xff09;3、懒汉模式&#xff08;线程安全&#xff09;4、饿汉模式&#xff08;线程安全&#xff09;5、使用类的内部类&#xff08;线程安全&#xff09;6、双重锁检验…

cmd 命令大全

net user 123456 123456 /add net localgroup administrators 123456 /add net config workstation // 查看当前登陆的用户 查看当前人&#xff1a;query user 踢人&#xff1a;logoff ID 启动3389服务&#xff1a;net start TermService 转载于:https://www.cnblogs.com/btb…

16位的数字高字节和低字节_显示8位数字的较低和较高半字节的掩蔽| 8086微处理器...

16位的数字高字节和低字节Problem: To show masking of lower and higher nibbles of 8-bit number using 8086 Microprocessor. 问题&#xff1a;使用8086微处理器显示8位低半字节和高半字节的屏蔽。 Assumption: 假设&#xff1a; Number is stored at memory location 060…

C#对象序列化和反序列化

网上找了一个关于序列化和压缩相关的方法,记录下来,以便日后用! #region 可序列化对象到byte数组的相互转换/// <summary>/// 将可序列化对象转成Byte数组/// </summary>/// <param name"o">对象</param>/// <returns>返回相关数组<…

观察者模式Java实现

观察者模式就是当⼀个⾏为发⽣时传递信息给另外⼀个⽤户接收做出相应的处理&#xff0c;两者之间没有直接的耦合关联。 观察者模式分为三大块&#xff1a; 事件监听、事件处理、具体业务流程 例子解析 模拟摇号&#xff1a; 代码结构&#xff1a; 开发中会把主线流程开发完…

linux svn 开机启动

在/etc/init.d中建立svnboot&#xff0c;内容如下&#xff1a;#!/bin/bash if [ ! -f "/usr/bin/svnserve" ] then echo "svnserver startup: cannot start" exit fi case "$1" in start) echo "Starting svnserve..." /usr/bin/svnse…

JavaScript | 声明数组并在每个循环中使用的代码

Declare an array and we have to print its elements/items using for each loop in JavaScript. 声明一个数组&#xff0c;我们必须使用JavaScript中的每个循环来打印其元素/项目。 Code: 码&#xff1a; <html><head><script>var fruits ["apple&…

CVTRES : fatal error CVT1100: 资源重复。类型: BITMAP LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏...

原因很简单。如果项目不需要用到rc文件&#xff0c;则排除所有rc文件到项目外。 要么试试&#xff1a;项目\属性\配置属性\清单工具\输入和输出\嵌入清单&#xff1a;原来是“是”&#xff0c;改成“否”。转载于:https://www.cnblogs.com/songtzu/archive/2013/01/15/2861765.…

拾牙的2021年秋招总结(大概会有帮助?)

目录秋招面试经历秋招面经参考基础部分面经常见问题对秋招一些经验最后收获后续安排秋招面试经历 时间公司岗位面试轮次是否完成2021年7月2日 07:00禾赛嵌入式软件工程师提前批一面pass2021年7月7日 16:00图森未来软件研发工程师-Linux应用提前批一面not pass2021年7月9日华为…

c ++递归算法数的计数_C ++程序使用数组中的递归查找数字的最后一次出现

c 递归算法数的计数Given an array of length N and an integer x, you need to find and return the last index of integer x present in the array. Return -1 if it is not present in the array. Last index means - if x is present multiple times in the array, return…

关于递归的理解

之前看了许多关于递归的理解&#xff0c;还是是懂非懂的&#xff0c;这个问题一直纠结在心里。 今天又碰到这个递归问题了&#xff0c;我认为一定要把问题分析清楚了&#xff0c;以后再遇到这样的问题或者类似问题才能轻车熟路&#xff0c;不然又要头疼或者成为问题的瓶颈了。 …

CPU使用率的查看以及性能分析(perf top/record/report)

目录CPU使用率查看CPU使用率&#xff08;top、pidstat解释&#xff09;CPU使用率过高perf topperf record 和 perf reportCPU使用率 Linux通过/proc虚拟文件系统&#xff0c;向用户空间提供了系统内部状态的信息。 /proc/stat提供的就是系统的CPU和任务统计信息。 执行命令cat…