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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
+ x22 + x23 <= 5; x31 + x32 + x33 <= 3; x11 + x12 + x13 <= 3; x21 + x22 + x23 <= 2; x31 + x32 + x33 <= 5; ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_N 26 #define INF 0xx11 + x21 <= 3; x12 + x22 <= 2; x13 + x23 <= 5; x313f3f3f3f int n; // 顶点量 char vertex[MAX_N + 1]; // + x32 <= 4; x21 + x31 <= 5; x22 + x32 <= 4; x23 + x 顶点组 int graph[MAX_N][MAX_N]; // 图 int vis[MAX_N]; // 标记是否访问33 <= 3; x11 >= 0; x12 >= 0; x13 >= 0; x21 >= 0; 过 char queue[MAX_N]; // 队列 int front = 0, rear = 0; // 队列头尾x22 >= 0; x23 >= 0; x31 >= 0; x32 >= 0; x33 >= 0指针 // 比较两个字符的大小 int cmp(char a, char b) { return a - b; } //; end ``` 分析和总结: 根据Matlab和Lingo的结果,该公司应该将3个产品 将字符 c 入队列 void enqueue(char c) { queue[rear++] = c; } // 出队列 char dequeue() { return queue[front++]; } // 判断队列是否为空 int is_empty() { return front == rear; } //从工厂A运输到仓库Y,将1个产品从工厂B运输到仓库X,将2个 广度优先搜索 void bfs(char start) { memset(vis, 0, sizeof(vis)); // 将 vis 组清产品从工厂B运输到仓库Z,将4个产品从工厂C运输到仓库X,将1零 front = rear = 0; // 初始化队列 int start_index = start - 'A'; // 起个产品从工厂C运输到仓库Y,将2个产品从工厂C运输到仓库Z。这始顶点的下标 vis[start_index] = 1; // 标记起始顶点已访问 样可以满足所有客户需求,同时总成本最小,即14。该运输计划可以帮助公司 enqueue(start); // 将起始顶点入队列 // 遍历队列中的元素 while (!is实现最优化运输,减少成本,提高效率。 3. 投资问题 假设一个投资_empty()) { char cur_vertex = dequeue(); // 取出队列头部元素 printf("%c ", cur_vertex);人有100万的可用资金,他可以将资金投资在股票、债券和房地产三种投资方式中。根据过去的经验,他预计在未来一年中,股票、债券和 // 访问该顶点 // 遍历当前顶点的邻接点 for (int i = 0;房地产的收益率分别为20%、10%和15%。他希望最大化收益。请 i < n; i++) { // 如果邻接点未被访问且与当前顶点相邻接 if (!问他应该如何进行资金分配? 建模过程: 目标函:maximize 0.2x1vis[i] && graph[cur_vertex - 'A'][i]) { vis[i] = 1; // 标记邻接点 + 0.1x2 + 0.15x3 约束条件: - x1 + x2 + x3 <=已访问 enqueue(vertex[i]); // 将邻接点入队列 } } } } int main() 100 - x1 >= 0 - x2 >= 0 - x3 >= 0 其中,x1表示 { scanf("%d", &n); // 读入顶点组 scanf("%s", vertex); // 读投资在股票的金额,x2表示投资在债券的金额,x3表示投资在房地产的入图的邻接矩阵 for (int i = 0; i < n; i++) { for (int j金额。 Matlab代码: ```matlab f = [-0.2 -0.1 -0.15]; A = [1 = 0; j < n; j++) { scanf("%d", &graph[i][j]); if (graph[i][j 1 1]; b = [100]; lb = [0 0 0]; ub = [Inf Inf Inf]; [x,fval] == 0 && i != j) { graph[i][j] = INF; // 无连接的顶点之间] = linprog(f,A,b,[],[],lb,ub) ``` Lingo代码: ```lingo max = 0.2距离为正无穷 } } } // Floyd 算法求任意两点之间的最短路径x1 + 0.1x2 + 0.15x3; x1 + x2 + x3 <= 100; for (int k = 0; k < n; k++) { for (int i = 0; i < nx1 >= 0; x2 >= 0; x3 >= 0; end ``` 分析和总结: 根; i++) { for (int j = 0; j < n; j++) { if (graph[i][k] +据Matlab和Lingo的结果,该投资人应该将40万投资在股票,30万投资在 graph[k][j] < graph[i][j]) { graph[i][j] = graph[i][k] + graph[k][j债券,30万投资在房地产。这样可以获得最大收益,即13万。这个模]; } } } } // 读入起始顶点 char start; scanf(" %c",型可以帮助投资人制定更加合理的资金分配策略,降低风险,提高收 &start); // 执行广度优先搜索 bfs(start); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值