📜  需要安装的最少灯具数量

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

需要安装的最少灯具数量

给定字符串str 仅包含点和星号。一个点代表自由空间和*               代表灯。一盏灯在位置i               可以在位置i-1、i和 i+1传播其光。确定照亮整个灯字符串所需的最少灯数。

例子:

方法:如果我们没有星号*               那么对于每 3 个点,我们需要一个灯,所以答案是ceil(D/3) ,其中 D 是点数。这个问题可以通过创建给定字符串的副本来解决,对于第一个字符串中的每个星号,我们在第二个字符串中的相邻索引处放置一个星号。
因此,如果给定的字符串是“...**..” ,那么第二个字符串将是“..****”。 .
之后,我们计算每个连续点块中的点数并找到该块所需的灯的数量,对于每个块,答案将是ceil(D/3) ,这些灯的总和将是回答完整的字符串。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
void check(int n, string s)
{
      
    // Create the modified string with
    // v[i-1] = v[i + 1] = * where s[i] = *
    char v[n];
    for(int i = 0; i < n; i++)
    {
        if (s[i] == '*')
        {
            v[i] = '*';
              
            // Checking valid index and then replacing
            // "." with "*" on the surrounding of a *
            if (i > 0 && i < n - 1)
            {
                v[i + 1] = '*';
                v[i - 1] = '*';
            }
            if (i == 0 && n != 1)
            {
                v[i + 1] = '*';
            }
            if (i == n - 1 && n != 1)
            {
                v[i - 1] = '*';
            }
        }
        else
        {
              
            // Just copying if the character is a "."
            if (v[i] != '*')
            {
                v[i] = '.';
            }
        }
    }
  
    // Creating the string with the list v
    string str(v);
     
    string word = "";
    char dl = '*';
  
    // to count the number of split strings
    int num = 0;
  
    // adding delimiter character at the end
    // of 'str'
    str = str + dl;
  
    // length of 'str'
    int l = str.size();
  
    // traversing 'str' from left to right
    vector res;
    for (int i = 0; i < l; i++) {
  
        // if str[i] is not equal to the delimiter
        // character then accumulate it to 'word'
        if (str[i] != dl)
            word = word + str[i];
  
        else {
  
            // if 'word' is not an empty string,
            // then add this 'word' to the array
            // 'substr_list[]'
            if ((int)word.size() != 0)
                res.push_back(word);
  
            // reset 'word'
            word = "";
        }
    }
  
    int ans = 0;
    for(auto x : res)
    {
       
        // Continuing if the string length is 0
        if (x.length() == 0)
        {
            continue;
        }
  
        // Adding number of lamps for each block of "."
        ans += ceil(x.length() * 1.0 / 3);
    }
    cout << ans << "\n";
}
 
int main() {
    string s = ".....";
    int n = s.length();
    check(n, s);
    return 0;
}
 
// This code is contributed by NishaBharti.


Java
// Java implementation of the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to print minimum amount
// of lamps needed to be installed
static void check(int n, String s)
{
     
    // Create the modified string with
    // v[i-1] = v[i + 1] = * where s[i] = *
    char v[] = new char[n];
    for(int i = 0; i < n; i++)
    {
        if (s.charAt(i) == '*')
        {
            v[i] = '*';
             
            // Checking valid index and then replacing
            // "." with "*" on the surrounding of a *
            if (i > 0 && i < n - 1)
            {
                v[i + 1] = '*';
                v[i - 1] = '*';
            }
            if (i == 0 && n != 1)
            {
                v[i + 1] = '*';
            }
            if (i == n - 1 && n != 1)
            {
                v[i - 1] = '*';
            }
        }
        else
        {
             
            // Just copying if the character is a "."
            if (v[i] != '*')
            {
                v[i] = '.';
            }
        }
    }
 
    // Creating the string with the list v
    String xx = new String(v);
 
    // Splitting the string into blocks
    // with "*" as delimiter
    String x[] = xx.split("\\*");
 
    int ans = 0;
    for(String xi : x)
    {
         
        // Continuing if the string length is 0
        if (xi.length() == 0)
        {
            continue;
        }
 
        // Adding number of lamps for each block of "."
        ans += Math.ceil(xi.length() * 1.0 / 3);
    }
    System.out.println(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "......";
    int n = s.length();
     
    check(n, s);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 implementation of the above approach
import math
 
# Function to print minimum amount
# of lamps needed to be installed
def check(n, s):
 
    # Create the modified string with
    # v[i-1] = v[i + 1] = * where s[i] = *
    v = [""] * n
    for i in range(n):
        if (s[i] == "*"):
            v[i] = "*"
 
            # Checking valid index and then replacing
            # "." with "*" on the surrounding of a *
            if (i > 0 and i < n - 1):
                v[i + 1] = "*"
                v[i - 1] = "*"
            if (i == 0 and n != 1):
                v[i + 1] = "*"
            if (i == n - 1 and n != 1):
                v[i - 1] = "*"
        else:
 
            # Just copying if the character is a "."
            if (v[i] != "*"):
                v[i] = "."
 
    # Creating the string with the list v
    xx = ''.join(v)
 
    # Splitting the string into blocks
    # with "*" as delimiter
    x = xx.split("*")
    s = 0
    for i in range(len(x)):
 
        # Continuing if the string length is 0
        if (x[i] == ""):
            continue
 
        # Adding number of lamps for each block of "."
        s += math.ceil(len(x[i]) / 3)
    print(s)
 
# Driver code
s = "......"
n = len(s)
check(n, s)


C#
// C# implementation of the above approach
using System;
class GFG {
 
    // Function to print minimum amount
    // of lamps needed to be installed
    static void check(int n, string s)
    {
 
        // Create the modified string with
        // v[i-1] = v[i + 1] = * where s[i] = *
        char[] v = new char[n];
        for (int i = 0; i < n; i++) {
            if (s[i] == '*') {
                v[i] = '*';
 
                // Checking valid index and then replacing
                // "." with "*" on the surrounding of a *
                if (i > 0 && i < n - 1) {
                    v[i + 1] = '*';
                    v[i - 1] = '*';
                }
                if (i == 0 && n != 1) {
                    v[i + 1] = '*';
                }
                if (i == n - 1 && n != 1) {
                    v[i - 1] = '*';
                }
            }
            else {
 
                // Just copying if the character is a "."
                if (v[i] != '*') {
                    v[i] = '.';
                }
            }
        }
 
        // Creating the string with the list v
        string xx = new string(v);
 
        // Splitting the string into blocks
        // with "*" as delimiter
        string[] x = xx.Split("\\*");
 
        int ans = 0;
        foreach(string xi in x)
        {
 
            // Continuing if the string length is 0
            if (xi.Length == 0) {
                continue;
            }
 
            // Adding number of lamps for each block of "."
            ans += (int)(Math.Ceiling(xi.Length * 1.0 / 3));
        }
        Console.Write(ans);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        string s = "......";
        int n = s.Length;
 
        check(n, s);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2