📜  两辆卡车发生碰撞的可能性

📅  最后修改于: 2021-04-22 08:46:22             🧑  作者: Mango

给定两个字符串ST ,其中S表示车辆从左向右移动的第一车道, T表示车辆从右向左移动的第二车道。车辆可以是B(自行车)C(汽车)T(卡车) 。任务是找到两辆卡车之间发生碰撞的可能性。

例子:

插图:

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

方法:请按照以下步骤解决问题:

  • 找到有利结果的总数作为碰撞总数(卡车之间的事故),并找到可能的结果总数(碰撞总数)作为事故的总数。
  • 初始化一个等于0的变量答案以存储冲突计数。
  • 计算字符串T中的卡车数量,并将其存储在可变数量中
  • 同时遍历字符串ST的字符:
    • 如果S [i]等于‘T’ ,则以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


输出:
0.194444

时间复杂度: O(N)
辅助空间: O(1)