📌  相关文章
📜  查找给出了每个球的初始方向的球之间发生的碰撞总数

📅  最后修改于: 2021-05-17 18:17:09             🧑  作者: Mango

给定N个球成一条线。每个球的初始方向由字符串表示,分别由左右方向的“ L”和“ R”组成。假设在碰撞后两个球都反转了方向,并且碰撞前后的速度将保持不变。计算发生的碰撞总数。
例子:

Input: str = "RRLL"
Output: 4

Explanation
After first collision: RLRL
After second collision: LRRL
After third collision: LRLR
After fourth collision: LLRR

Input: str = "RRLR"
Output:  2

Explanation
After first collision: RLRR
After second collision: LRRR

方法:
在每个阶段,我们都必须找到子字符串“ RL”的编号,将子字符串“ RL”更改为“ LR”,并再次计算结果字符串中子字符串“ RL”的编号,然后重复以下操作,直到没有其他子字符串“ RL”可用。我们不会在每个阶段都计算子字符串“ RL”的数量,因为它将消耗大量时间。这样做的另一种方法是,观察在每个阶段看到的结果字符串–>“ RRLL”->“ RLRL”->“ LRLR”->“ LLRR”。
初始字符串为“ RRLL”。让我们有一个方向为L的第三个球。现在观察该L向字符串的左侧移动。当我们将“ RL”交换为“ LR”时,我们将L移向整个字符串的左侧。现在类似地,如果我们继续进行交换,则该L将面对该字符串左侧存在的每个R,因此再次形成“ RL”。因此,对于此L,“ RL”的总编号将是该特定L左侧存在的R的总编号。我们将对每个L重复此操作。因此,通常,为了计算所有可能的值在每个阶段的子字符串“ RL”中,我们将计算每个L左侧存在的R的总数,因为这些R和L将在每个阶段形成“ RL”,这可以线性时间复杂度完成。
下面是上述方法的实现:

C++
// C++ implementation to Find total
// no of collisions taking place between
// the balls in which initial direction
// of each ball is given
 
#include 
using namespace std;
 
// Function to count no of collision
int count(string s)
{
    int N, i, cnt = 0, ans = 0;
 
    // length of the string
    N = s.length();
 
    for (i = 0; i < N; i++) {
        if (s[i] == 'R')
            cnt++;
 
        if (s[i] == 'L')
            ans += cnt;
    }
 
    return ans;
}
 
// Driver code
int main()
{
 
    string s = "RRLL";
 
    cout << count(s) << endl;
 
    return 0;
}


Java
// Java implementation to Find total
// no of collisions taking place between
// the balls in which initial direction
// of each ball is given
import java.io.*;
class GFG {
      
// Function to count
// no of collision
static int count(String s)
{
  int N, i, cnt = 0, ans = 0;
 
  // length of the string
  N = s.length();
 
  for (i = 0; i < N; i++)
  {
    if (s.charAt(i) == 'R')
      cnt++;
 
    if (s.charAt(i) == 'L')
      ans += cnt;
  }
  return ans;
}
  
// Driver code
static public void main(String[] args)
{
  String s = "RRLL";
  System.out.println(count(s));
}
}
 
// This code is contributed by Rutvik_56


Python3
# Python3 implementation to find total
# no of collisions taking place between
# the balls in which initial direction
# of each ball is given
 
# Function to count no of collision
def count(s):
 
    cnt, ans = 0, 0
 
    # Length of the string
    N = len(s)
 
    for i in range(N):
        if (s[i] == 'R'):
            cnt += 1
 
        if (s[i] == 'L'):
            ans += cnt
     
    return ans
 
# Driver code
s = "RRLL"
 
print( count(s))
 
# This code is contributed by chitranayal


C#
// C# implementation to Find total
// no of collisions taking place between
// the balls in which initial direction
// of each ball is given
using System;
 
class GFG{
 
// Function to count no of collision
static int count(String s)
{
    int N, i, cnt = 0, ans = 0;
 
    // length of the string
    N = s.Length;
 
    for(i = 0; i < N; i++)
    {
        if (s[i] == 'R')
            cnt++;
 
        if (s[i] == 'L')
            ans += cnt;
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    String s = "RRLL";
 
    Console.Write(count(s));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
4