📜  查找信号到达字符串中所有位置所需的时间

📅  最后修改于: 2022-05-13 01:57:06.783000             🧑  作者: Mango

查找信号到达字符串中所有位置所需的时间

给定字符串“x”和“o”。从每个“x”发出一个信号,该信号在两个方向上传播。信号到达下一个单元需要一个单位的时间。如果单元格包含 'o',则信号将其更改为 'x' 。任务是计算将数组转换为信号字符串所需的时间。
字符串信号中仅包含“x”。
例子:

来源:OYO Rooms 采访集2
这个想法是找到最长的连续'o'的长度。还要检查,如果“x”出现在连续“o”的右侧和左侧,则时间段将减少到最大长度的一半,否则时间段将等于最大长度。
下面是上述方法的实现。

C++
// C++ program to Find time taken for signal
// to reach all positions in a string
#include 
using namespace std;
 
// Returns time needed for signal to traverse
// through complete string.
int maxLength(string s, int n)
{
    int right = 0, left = 0;
    int coun = 0, max_length = INT_MIN;
 
    s = s + '1'; // for the calculation of last index
    // for strings like oxoooo, xoxxoooo ..
 
    for (int i = 0; i <= n; i++) {
        if (s[i] == 'o')
            coun++;
 
        else {
 
            // if coun is greater than max_length
            if (coun > max_length) {
 
                right = 0;
                left = 0;
 
                // if 'x' is present at the right side
                // of max_length
                if (s[i] == 'x')
                    right = 1;
 
                // if 'x' is present at left side of
                // max_length
                if (((i - coun) > 0) && (s[i - coun - 1] == 'x'))
                    left = 1;
 
                // We use ceiling function to handle odd
                // number 'o's
                coun = ceil((double)coun / (right + left));
 
                max_length = max(max_length, coun);
            }
            coun = 0;
        }
    }
 
    return max_length;
}
 
// Driver code
int main()
{
    string s = "oooxoooooooooxooo";
    int n = s.size();
    cout << maxLength(s, n);
    return 0;
}


Java
// Java program to Find time taken for signal
// to reach all positions in a string
public class GFG {
 
    // Returns time needed for signal to traverse
    // through complete string.
    static int maxLength(String s, int n)
    {
        int right = 0, left = 0;
        int coun = 0, max_length = Integer.MIN_VALUE;
 
        s = s + '1'; // for the calculation of last index
        // for strings like oxoooo, xoxxoooo ..
 
        for (int i = 0; i <= n; i++) {
            if (s.charAt(i) == 'o')
                coun++;
 
            else {
 
                // if coun is greater than max_length
                if (coun > max_length) {
 
                    right = 0;
                    left = 0;
 
                    // if 'x' is present at the right side
                    // of max_length
                    if (s.charAt(i) == 'x')
                        right = 1;
 
                    // if 'x' is present at left side of
                    // max_length
                    if (((i - coun) > 0) && (s.charAt(i - coun - 1) == 'x'))
                        left = 1;
 
                    // We use ceiling function to handle odd
                    // number 'o's
                    coun = (int)Math.ceil((double)coun / (right + left));
 
                    max_length = Math.max(max_length, coun);
                }
                coun = 0;
            }
        }
 
        return max_length;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String s = "oooxoooooooooxooo";
        int n = s.length();
        System.out.println(maxLength(s, n));
    }
    // This code is contributed by ANKITRAI1
}


Python3
# Python3 program to Find time taken for signal
# to reach all positions in a string
import sys
import math
 
# Returns time needed for signal
# to traverse through complete string.
def maxLength(s, n):
    right = 0
    left = 0
    coun = 0
    max_length = -(sys.maxsize-1)
     
    # for the calculation of last index
    s = s+'1'
     
    # for strings like oxoooo, xoxxoooo 
    for i in range(0, n + 1):
        if s[i]=='o':
            coun+= 1
        else:
             
            # if coun is greater than
            # max_length
            if coun>max_length:
                right = 0
                left = 0
                 
                # if 'x' is present at the right side
                # of max_length
                if s[i]=='x':
                    right = 1
                     
                # if 'x' is present at left side of
                # max_length
                if i-coun>0 and s[i-coun-1] == 'x':
                    left = 1
                     
                # We use ceiling function to handle odd
                # number 'o's
                coun = math.ceil(float(coun/(right + left)))
                max_length = max(max_length, coun)
            coun = 0
 
    return max_length
 
# Driver code
if __name__=='__main__':
    s = "oooxoooooooooxooo"
    n = len(s)
    print(maxLength(s, n))
 
# This code is contributed by
# Shrikant13


C#
// C# program to Find time taken
// for signal to reach all
// positions in a string
using System;
 
class GFG {
 
    // Returns time needed for signal
    // to traverse through complete string.
    static int maxLength(string s, int n)
    {
        int right = 0, left = 0;
        int coun = 0, max_length = int.MinValue;
 
        s = s + '1'; // for the calculation of last index
 
        // for strings like oxoooo, xoxxoooo ..
        for (int i = 0; i <= n; i++) {
            if (s[i] == 'o')
                coun++;
 
            else {
 
                // if coun is greater than max_length
                if (coun > max_length) {
                    right = 0;
                    left = 0;
 
                    // if 'x' is present at the right
                    // side of max_length
                    if (s[i] == 'x')
                        right = 1;
 
                    // if 'x' is present at left side
                    // of max_length
                    if (((i - coun) > 0) && (s[i - coun - 1] == 'x'))
                        left = 1;
 
                    // We use ceiling function to
                    // handle odd number 'o's
                    coun = (int)Math.Ceiling((double)coun / (right + left));
 
                    max_length = Math.Max(max_length, coun);
                }
                coun = 0;
            }
        }
 
        return max_length;
    }
 
    // Driver code
    public static void Main()
    {
        string s = "oooxoooooooooxooo";
        int n = s.Length;
        Console.Write(maxLength(s, n));
    }
}
 
// This code is contributed
// by ChitraNayal


PHP
 $max_length)
            {
 
                $right = 0;
                $left = 0;
 
                // if 'x' is present at the right side
                // of max_length
                if ($s[$i] == 'x')
                    $right = 1;
 
                // if 'x' is present at left side of
                // max_length
                if ((($i - $coun) > 0) &&
                    ($s[$i - $coun - 1] == 'x'))
                    $left = 1;
 
                // We use ceiling function to handle odd
                // number 'o's
                $coun = (int)ceil((double)$coun / ($right +
                                                   $left));
 
                $max_length = max($max_length, $coun);
            }
            $coun = 0;
        }
    }
 
    return $max_length;
}
 
// Driver code
$s = "oooxoooooooooxooo";
$n = strlen($s);
echo(maxLength($s, $n));
 
// This code is contributed by Code_Mech


Javascript


输出:
5