在給定約束下可以使用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.pswp.cn/news/376918.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/376918.shtml
英文地址,請注明出處:http://en.pswp.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…