给定两个字符串S和T ,其中S表示车辆从左向右移动的第一条车道, T表示车辆从右向左移动的第二条车道。车辆可以是B(自行车) 、 C(汽车)或T(卡车) 。任务是找出两辆卡车发生碰撞的概率。
例子:
Input: S = “TCCBCTTB”, T = “BTCCBBTT”
Output: 0.194444
Explanation:
Total collision = 7
Total accident = 36
Therefore, the probability can be calculated by 7/36 = 0.19444.
Input: S = “BTT”, T = “BTCBT”
Output: 0.25000
插图:
S = "TCCBCTTB", T = "BTCCBBTT"
Possible cases | Accidents | Collision
-----------------------------------------
TCCBCTTB | |
BTCCBBTT | 8 | 1
| |
TCCBCTTB | |
BTCCBBTT | 7 | 2
| |
TCCBCTTB | |
BTCCBBTT | 6 | 1
| |
TCCBCTTB | |
BTCCBBTT | 5 | 0
| |
TCCBCTTB | |
BTCCBBTT | 4 | 0
| |
TCCBCTTB | |
BTCCBBTT | 3 | 0
| |
TCCBCTTB | |
BTCCBBTT | 2 | 1
| |
TCCBCTTB | |
BTCCBBTT | 1 | 1
Total number of accidents: 8+7+6+5+4+3+2+1=36
Total number of collision: 1+2+1+0+0+0+1+1=7
Probability: 7/36=0.19444
处理方法:按照以下步骤解决问题:
- 求有利结果的总数作为碰撞总数(卡车之间的事故)和可能结果的总数(碰撞总数)作为事故总数。
- 将变量answer初始化为0以存储冲突计数。
- 计算字符串T中卡车的数量并将其存储在变量count 中。
- 同时迭代字符串S和T的字符:
- 如果S[i]等于‘T’ ,则将answer增加count 。
- 如果T[i]等于 ‘T’,则将计数减1 。
- 现在,计算可能结果的总数(事故总数)。如果保持字符串a向右移动或字符串b向左移动一个单位,则为所有重叠长度的总和。
- 设字符串的长度为N ,字符串b 为M 。那么,重叠的总数将是:
- 如果N > M那么,它将是前 M 个自然数的总和,即M*(M + 1)/2 。
- 否则,它将是N*(N + 1)/2 + (M – N)*N 。
- 求概率为碰撞次数与事故次数之比。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate total
// number of accidents
double count_of_accident(string a,
string b)
{
// String size
int n = a.size(), m = b.size();
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
// Function to calculate count
// of all possible collision
double count_of_collision(string a,
string b)
{
int n = a.size(), m = b.size();
// Stores the count of collisions
int answer = 0;
// Total number of truck in lane b
int count_of_truck_in_lane_b = 0;
for (int i = 0; i < m; i++)
if (b[i] == 'T')
count_of_truck_in_lane_b++;
// Count total number of collisions
// while traversing the string a
for (int i = 0; i < n && i < m; i++) {
if (a[i] == 'T')
answer
+= count_of_truck_in_lane_b;
if (b[i] == 'T')
count_of_truck_in_lane_b--;
}
return answer;
}
// Function to calculate the
// probability of collisions
double findProbability(string a,
string b)
{
// Evaluate total outcome that is
// all the possible accident
double total_outcome
= count_of_accident(a, b);
// Evaluate favourable outcome i.e.,
// count of collision of trucks
double favourable_outcome
= count_of_collision(a, b);
// Print desired probabilty
cout << favourable_outcome
/ total_outcome;
}
// Driver Code
int main()
{
string S = "TCCBCTTB", T = "BTCCBBTT";
// Function Call
findProbability(S, T);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to calculate total
// number of accidents
static int count_of_accident(String a,
String b)
{
// String size
int n = a.length(), m = b.length();
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
// Function to calculate count
// of all possible collision
static double count_of_collision(String a,
String b)
{
int n = a.length(), m = b.length();
// Stores the count of collisions
double answer = 0;
// Total number of truck in lane b
int count_of_truck_in_lane_b = 0;
for (int i = 0; i < m; i++)
if (b.charAt(i) == 'T')
count_of_truck_in_lane_b++;
// Count total number of collisions
// while traversing the String a
for (int i = 0; i < n && i < m; i++)
{
if (a.charAt(i) == 'T')
answer
+= count_of_truck_in_lane_b;
if (b.charAt(i) == 'T')
count_of_truck_in_lane_b--;
}
return answer;
}
// Function to calculate the
// probability of collisions
static void findProbability(String a,
String b)
{
// Evaluate total outcome that is
// all the possible accident
int total_outcome
= count_of_accident(a, b);
// Evaluate favourable outcome i.e.,
// count of collision of trucks
double favourable_outcome
= count_of_collision(a, b);
// Print desired probabilty
System.out.printf("%4f",favourable_outcome
/ total_outcome);
}
// Driver Code
public static void main(String[] args)
{
String S = "TCCBCTTB", T = "BTCCBBTT";
// Function Call
findProbability(S, T);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to calculate total
# number of accidents
def count_of_accident(a, b):
n = len(a)
m = len(b)
if (n > m):
return (m * (m + 1)) / 2
else:
return ((n * (n + 1)) / 2 +
(m - n) * n)
# Function to calculate count
# of all possible collision
def count_of_collision(a, b):
# Size of string
n = len(a)
m = len(b)
# Stores the count of collisions
answer = 0
# Total number of truck in lane b
count_of_truck_in_lane_b = 0
for i in range(0, m):
if (b[i] == 'T'):
count_of_truck_in_lane_b += 1
# Count total number of collisions
# while traversing the string a
i = 0
while (i < m and i < n):
if (a[i] == 'T'):
answer += count_of_truck_in_lane_b
if (b[i] == 'T'):
count_of_truck_in_lane_b -= 1
i += 1
return answer
# Function to calculate the
# probability of collisions
def findProbability(a, b):
# Evaluate total outcome that is
# all the possible accident
total_outcome = count_of_accident(a, b);
# Evaluate favourable outcome i.e.,
# count of collision of trucks
favourable_outcome = count_of_collision(a, b);
# Print desired probabilty
print(favourable_outcome / total_outcome)
# Driver Code
if __name__ == "__main__" :
S = "TCCBCTTB"
T = "BTCCBBTT"
# Function Call
findProbability(S, T)
# This code is contributed by Virusbuddah_
C#
// C# program for the above approach
using System;
class GFG
{
// Function to calculate total
// number of accidents
static int count_of_accident(String a,
String b)
{
// String size
int n = a.Length, m = b.Length;
if (n > m)
return (m * (m + 1)) / 2;
else
return (n * (n + 1))
/ 2
+ (m - n) * n;
}
// Function to calculate count
// of all possible collision
static double count_of_collision(String a,
String b)
{
int n = a.Length, m = b.Length;
// Stores the count of collisions
double answer = 0;
// Total number of truck in lane b
int count_of_truck_in_lane_b = 0;
for (int i = 0; i < m; i++)
if (b[i] == 'T')
count_of_truck_in_lane_b++;
// Count total number of collisions
// while traversing the String a
for (int i = 0; i < n && i < m; i++)
{
if (a[i] == 'T')
answer
+= count_of_truck_in_lane_b;
if (b[i] == 'T')
count_of_truck_in_lane_b--;
}
return answer;
}
// Function to calculate the
// probability of collisions
static void findProbability(String a,
String b)
{
// Evaluate total outcome that is
// all the possible accident
int total_outcome
= count_of_accident(a, b);
// Evaluate favourable outcome i.e.,
// count of collision of trucks
double favourable_outcome
= count_of_collision(a, b);
// Print desired probabilty
Console.Write("{0:F4}", favourable_outcome
/ total_outcome);
}
// Driver Code
public static void Main(String[] args)
{
String S = "TCCBCTTB", T = "BTCCBBTT";
// Function Call
findProbability(S, T);
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
0.194444
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live