📜  写入给定字符串所需的行数

📅  最后修改于: 2021-10-26 06:47:30             🧑  作者: Mango

给定一个字符串str和一个整数数组width[]其中:

任务是找到将字符串str写在纸上所需的行数以及它被占用的最后一行的宽度。
注:线宽为10 个单位
例子:

做法:我们将字符串str中的每个字符一个一个地写出来。当我们写一个字符,我们会立即更新 (lines, width) 以跟踪我们到目前为止使用了多少行以及最后一行中使用的空间的长度是多少。
如果str 中width[char]适合我们当前的行,我们将添加它。否则,我们将从新行开始
下面是上述方法的实现:

C++
// CPP implementation of the approach
#include 
using namespace std;
 
// Function to return the number of lines required
pair numberOfLines(string S, int *widths)
{
    // If string is empty
    if (S.empty())
        return {0, 0};
 
    // Initialize lines and width
    int lines = 1, width = 0;
 
    // Iterate through S
    for (auto character : S)
    {
        int w = widths[character - 'a'];
        width += w;
 
        if (width >= 10)
        {
            lines++;
            width = w;
        }
    }
 
    // Return lines and width used
    return {lines, width};
}
 
// Driver Code
int main()
{
    string S = "bbbcccdddaa";
    int widths[] = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
 
    // Function call to print required answer
    pair ans = numberOfLines(S, widths);
    cout << ans.first << " " << ans.second << endl;
 
    return 0;
}
 
// This code is contributed by
// sanjeev2552


Java
// JAVA implementation of the approach
class GFG
{
 
// Function to return the number of lines required
static int[] numberOfLines(String S, int []widths)
{
    // If String is empty
    if (S.isEmpty())
        return new int[]{0, 0};
 
    // Initialize lines and width
    int lines = 1, width = 0;
 
    // Iterate through S
    for (char character : S.toCharArray())
    {
        int w = widths[character - 'a'];
        width += w;
 
        if (width >= 10)
        {
            lines++;
            width = w;
        }
    }
 
    // Return lines and width used
    return new int[]{lines, width};
}
 
// Driver Code
public static void main(String[] args)
{
    String S = "bbbcccdddaa";
    int widths[] = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
 
    // Function call to print required answer
    int []ans = numberOfLines(S, widths);
    System.out.print(ans[0]+ " " + ans[1] +"\n");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the number of lines required
def numberOfLines(S, widths):
 
    # If string is empty
    if(S == ""):
        return 0, 0
 
    # Initialize lines and width
    lines, width = 1, 0
 
    # Iterate through S
    for c in S:
        w = widths[ord(c) - ord('a')]
        width += w
        if width > 10:
            lines += 1
            width = w
 
    # Return lines and width used
    return lines, width
 
 
# Driver Code
S = "bbbcccdddaa"
Widths = [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
 
# Function call to print required answer
print(numberOfLines(S, Widths))


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to return the number of lines required
static int[] numberOfLines(String S, int []widths)
{
    // If String is empty
    if (S.Length == 0)
        return new int[]{0, 0};
 
    // Initialize lines and width
    int lines = 1, width = 0;
 
    // Iterate through S
    foreach (char character in S.ToCharArray())
    {
        int w = widths[character - 'a'];
        width += w;
 
        if (width >= 10)
        {
            lines++;
            width = w;
        }
    }
 
    // Return lines and width used
    return new int[]{lines, width};
}
 
// Driver Code
public static void Main(String[] args)
{
    String S = "bbbcccdddaa";
    int []widths = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
 
    // Function call to print required answer
    int []ans = numberOfLines(S, widths);
    Console.Write(ans[0]+ " " + ans[1] +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
(2, 8)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程