日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在给定约束下可以使用a,b和c形成的字符串数

發布時間:2023/12/1 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在给定约束下可以使用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=1Output:3Possible strings are:"a", "b", "c"Input: n=2Output:8Possible 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中的任何一位。

  • .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    Example with explanation

    帶說明的例子

    Let n=2Case 1: String is only built with 'a', i.e., n 'a' forms the string"aa"Total under this category: 1Case 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 categoryTotal under this category: 0Case 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 : 8Second 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形成的字符串数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。