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