[codesignal] textJustification

Description

Given an array of words and a length l, format the text such that each line has exactly l characters and is fully justified on both the left and the right. Words should be packed in a greedy approach; that is, pack as many words as possible in each line. Add extra spaces when necessary so that each line has exactly l characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right. For the last line of text and lines with one word only, the words should be left justified with no extra space inserted between them.

Example

For words = ["This", "is", "an", "example", "of", "text", "justification."] and l = 16, the output should be

textJustification(words, l) = ["This    is    an",
                               "example  of text",
                               "justification.  "]

Input/Output

  • [execution time limit] 0.5 seconds (cpp)

  • [input] array.string words

    • An array of words. Each word is guaranteed not to exceed l in length.
    • Guaranteed constraints:
      • 1 ≤ words.length ≤ 150,
      • 0 ≤ words[i].length ≤ l.
  • [input] integer l

    • The length that all the lines in the output array should be.
    • Guaranteed constraints:
      • 1 ≤ l ≤ 60.
  • [output] array.string

    • The formatted text as an array containing lines of text, with each line having a length of l.

solution

大致思想是首先计算一行能够容纳多少个单词,在计算容纳单词数时需要加上单词与单词之间的空格。然后如果一行中单词加上单词之间的一个空格恰好等于 l,则直接将临时字符串存入结果数组。如果小于,则需要在单词之间补充额外的空格,具体数量如下:

  • 首先用一行的长度 l 减去该行的单词数量得到总的需要添加的空格数,记为space
  • 假设单词数量为 n, 则需要在单词之间补充 n - 1 组空格(每一组的空格数可能不等)
  • 那么每一组都需要补充 space / (n - 1) 个空格
  • 剩余的前 space % (n - 1) 处还需要再补充一个空格
#include <iostream>
#include <vector>

std::vector<std::string> textJustification(std::vector<std::string>& words, int l) {
    std::vector<std::string> res;
    for (int i = 0; i < words.size();) {
        int start = i;
        std::string temp = words[i];
        i++;
        while(i < words.size() && (temp + " " + words[i]).length() <= l) {
            temp += " " + words[i];
            i++;
        }
        if (temp.length() == l) {  // 如果长度刚好,则不需要扩充空格
            res.emplace_back(temp);
        } else {
            if (i - start == 1 || i >= words.size()) { // 如果只有一个单词或者单词已用完
                while (temp.length() < l) temp += " ";  // 需要补充空格
                res.emplace_back(temp);
            } else {
                int spaces = l - temp.length() + i - start - 1;  // 一行中需要补充的空格数量
                int perWord = spaces / (i - start - 1); // 每个原始有空格的地方需要添加的空格数
                int extras = spaces % (i - start - 1);  // 还需要补充空格的前 extras 处
                temp = "";
                for (int j = start; j < i-1; j++) {
                    temp += words[j];
                    for (int k = 0; k < perWord; ++k) {
                        temp += " ";
                    }
                    if (j - start < extras) temp += " ";
                }
                temp += words[i-1];
                res.emplace_back(temp);
            }
        }
    }
    return res;
}

template<typename T>
void print1DVector(const std::string& str, std::vector<T>& array) {
    std::cout << str << ": ";
    for (auto arr: array) {
        std::cout << arr << '\n';
    }
}

int main() {
    std::vector<std::string> words{"This", "is", "an", "example", "of", "text", "justification."};
    std::vector<std::string> res  = textJustification(words, 16);
    print1DVector("Justifi text", res);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值