📜  查找给定时间的队列安排

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

n人正排着队购买狂欢节的入场券。在场的人们强烈相信骑士精神。因此,在时间= t处,如果位置x处的一个男人发现一个站在他身后的女人,则他与她交换了位置,因此,在时间= t + 1处,女人正站在位置x上,而男人则站在她身后。
给定队列中的总人数为n,特定时刻为t,并且队列的初始排列为字符串形式,其中字符串“ M”代表男人在位置i,“ W”代表女人在位置i,找出时间= t时的队列安排。

例子 :

Input : n = 6, t = 2
       BBGBBG
Output: GBBGBB
Explanation:
At t = 1, 'B' at position 2 will swap
with 'G' at position 3 and 'B' at 
position 5 will swap with 'G' at 
position 6. String after t = 1 changes 
to "BGBBGB". Now at t = 2, 'B' at
position = 1 will swap with 'G' at 
position = 2 and 'B' at position = 4 
will swap with 'G' at position 5. 
String changes to "GBBGBB". Since, 
we have to display arrangement at
t = 2, the current arrangement is 
our answer. 

Input : n = 8, t = 3
       BBGBGBGB
Output: GGBGBBBB

方法:
在从1到t的每个时刻遍历整个字符串,如果找到成对的“ BG”,则交换它们并移动以检查下一个对。

下面是上述方法的实现:

C++
// CPP program to find the arrangement
// of queue at time = t
#include 
using namespace std;
  
// prints the arrangement at time = t
void solve(int n, int t, string s)
{
    // Checking the entire queue for 
    // every moment from time = 1 to
    // time = t.
    for (int i = 0; i < t; i++) 
        for (int j = 0; j < n - 1; j++) 
          
            /*If current index contains 'B' 
              and next index contains 'G' 
              then swap*/
            if (s[j] == 'B' && s[j + 1] == 'G') {
                char temp = s[j];
                s[j] = s[j + 1];
                s[j + 1] = temp;
                j++;
            }
              
    cout << s;
}
  
// Driver function for the program
int main()
{
    int n = 6, t = 2;
    string s = "BBGBBG";
    solve(n, t, s);
    return 0;
}


Java
// Java program to find the arrangement
// of queue at time = t
import java.io.*;
  
class Geek {
      
    // prints the arrangement at time = t
    static void solve(int n, int t, char s[])
    {
        // Checking the entire queue for 
        // every moment from time = 1 to 
        // time = t.
        for (int i = 0; i < t; i++) 
            for (int j = 0; j < n - 1; j++) 
              
                /*If current index contains 'B' 
                  and next index contains 'G' 
                  then swap.*/
                if (s[j] == 'B' && s[j + 1] == 'G') {
                    char temp = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                    j++;
                }
                  
        System.out.print(s);
    }
      
    // Driver function
    public static void main(String args[])
    {
        int n = 6, t = 2;
        String s = "BBGBBG";
        char str[] = s.toCharArray();
        solve(n, t, str);
    }
}


Python3
# Python program to find 
# the arrangement of 
# queue at time = t
  
# prints the arrangement
# at time = t
def solve(n, t, p) :
      
    s = list(p)
      
    # Checking the entire 
    # queue for every
    # moment from time = 1
    # to time = t.
    for i in range(0, t) :
      
        for j in range(0, n - 1) :     
          
            # If current index 
            # contains 'B' and
            # next index contains 
            # 'G' then swap
            if (s[j] == 'B' and
                s[j + 1] == 'G') :
                  
                temp = s[j];
                s[j] = s[j + 1];
                s[j + 1] = temp;
                j = j + 1    
                  
    print (''.join(s))
  
# Driver code
n = 6
t = 2
p = "BBGBBG"
solve(n, t, p)
  
# This code is contributed by 
# Manish Shaw(manishshaw1)


C#
// C# program to find the arrangement
// of queue at time = t
using System;
  
class Geek {
      
    // prints the arrangement at time = t
    static void solve(int n, int t, char[] s)
    {
        // Checking the entire queue for 
        // every moment from time = 1 to 
        // time = t.
        for (int i = 0; i < t; i++) 
            for (int j = 0; j < n - 1; j++) 
              
                /*If current index contains 'B' 
                and next index contains 'G' 
                then swap.*/
                if (s[j] == 'B' && s[j + 1] == 'G') 
                {
                    char temp = s[j];
                    s[j] = s[j + 1];
                    s[j + 1] = temp;
                    j++;
                }
                  
        Console.Write(s);
    }
      
    // Driver function
    public static void Main(String[] args)
    {
        int n = 6, t = 2;
        String s = "BBGBBG";
        char []str = s.ToCharArray();
        solve(n, t, str);
    }
}
  
// This code is contributed by parashar...


PHP


输出:

GBBGBB

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。