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