{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "00593f4b57704a038b0429d5fe708031",
  "keywords": "字符串,模拟",
  "title": "文本左右对齐",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给定一个单词数组和一个长度&nbsp;<em>maxWidth</em>，重新排版单词，使其成为每行恰好有&nbsp;<em>maxWidth</em>&nbsp;个字符，且左右两端对齐的文本。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>你应该使用“贪心算法”来放置给定的单词；也就是说，尽可能多地往每行中放置单词。必要时可用空格&nbsp;<code>' '</code>&nbsp;填充，使得每行恰好有 <em>maxWidth</em>&nbsp;个字符。\n</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配，则左侧放置的空格数要多于右侧的空格数。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>文本的最后一行应为左对齐，且单词之间不插入<strong>额外的</strong>空格。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>说明:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li>单词是指由非空格字符组成的字符序列。</li>\n<li>每个单词的长度大于 0，小于等于&nbsp;<em>maxWidth</em>。</li>\n<li>输入单词数组 <code>words</code>&nbsp;至少包含一个单词。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>\nwords = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\nmaxWidth = 16\n<strong>输出:</strong>\n[\n&nbsp; &nbsp;\"This &nbsp; &nbsp;is &nbsp; &nbsp;an\",\n&nbsp; &nbsp;\"example &nbsp;of text\",\n&nbsp; &nbsp;\"justification. &nbsp;\"\n]\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;2:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>\nwords = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\nmaxWidth = 16\n<strong>输出:</strong>\n[\n&nbsp; \"What &nbsp; must &nbsp; be\",\n&nbsp; \"acknowledgment &nbsp;\",\n&nbsp; \"shall be &nbsp; &nbsp; &nbsp; &nbsp;\"\n]\n<strong>解释: </strong>注意最后一行的格式应为 \"shall be    \" 而不是 \"shall     be\"\n因为最后一行应为左对齐，而不是左右两端对齐，第二行同样为左对齐，这是因为这行只包含一个单词。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例&nbsp;3:</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入:</strong>\nwords = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n&nbsp;        \"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\nmaxWidth = 20\n<strong>输出:</strong>\n[\n&nbsp; \"Science &nbsp;is &nbsp;what we\",\n\"understand &nbsp; &nbsp; &nbsp;well\",\n&nbsp; \"enough to explain to\",\n&nbsp; \"a &nbsp;computer. &nbsp;Art is\",\n&nbsp; \"everything &nbsp;else &nbsp;we\",\n&nbsp; \"do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"\n]\n</pre>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\nstatic void line_fill(char *line, int len, char **words, int *word_lens, int max_size,\n\t\t\t\t\t  int even_spaces, int remain_spaces, int start, int end)\n{\n\tint i, j;\n\tchar *p = line;\n\tfor (i = start; i < end; i++)\n\t{\n\t\tmemcpy(p, words[i], word_lens[i]);\n\t\tp += word_lens[i];\n\t\tif (i < end - 1)\n\t\t{\n\t\t\tfor (j = 0; j < even_spaces; j++)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t\tif (remain_spaces > 0)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t\tremain_spaces--;\n\t\t\t}\n\t\t}\n\t}\n\twhile (p - line < max_size)\n\t{\n\t\t*p++ = ' ';\n\t}\n\t*p++ = '\\0';\n}\nstatic char **fullJustify(char **words, int wordsSize, int maxWidth, int *returnSize)\n{\n\tint i, j, k, cap = 100, count = 0;\n\tchar **lines = malloc(cap * sizeof(char *));\n\tchar *buf = malloc(cap * (maxWidth + 1));\n\tfor (i = 0; i < cap; i++)\n\t{\n\t\tlines[i] = buf + i * (maxWidth + 1);\n\t}\n\tint *word_lens = malloc(wordsSize * sizeof(int));\n\tfor (i = 0; i < wordsSize; i++)\n\t{\n\t\tword_lens[i] = strlen(words[i]);\n\t}\n\tint wc = 0;\n\tint len = 0;\n\tint start = 0;\n\tint chars = 0;\n\tfor (i = 0, j = 0; i < wordsSize; i++)\n\t{\n\t\tif (len + word_lens[i] > maxWidth)\n\t\t{\n\t\t\tint even_spaces = wc == 1 ? 0 : (maxWidth - chars) / (wc - 1);\n\t\t\tint remain_spaces = wc == 1 ? 0 : (maxWidth - chars) % (wc - 1);\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i);\n\t\t\tcount++;\n\t\t\twc = 1;\n\t\t\tlen = word_lens[i] + 1;\n\t\t\tchars = word_lens[i];\n\t\t\tstart = i;\n\t\t}\n\t\telse if (len + word_lens[i] == maxWidth)\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tint even_spaces = wc == 0 ? 0 : (maxWidth - chars) / wc;\n\t\t\tint remain_spaces = wc == 0 ? 0 : (maxWidth - chars) % wc;\n\t\t\tline_fill(lines[count], len, words, word_lens, maxWidth, even_spaces, remain_spaces, start, i + 1);\n\t\t\tcount++;\n\t\t\twc = 0;\n\t\t\tlen = 0;\n\t\t\tchars = 0;\n\t\t\tstart = i + 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t\tchars += word_lens[i];\n\t\t\tlen += word_lens[i] + 1;\n\t\t\twc++;\n\t\t}\n\t}\n\tif (wc > 0)\n\t{\n\t\tchar *p = lines[count];\n\t\tfor (i = start; i < start + wc; i++)\n\t\t{\n\t\t\tmemcpy(p, words[i], word_lens[i]);\n\t\t\tp += word_lens[i];\n\t\t\tif (i < start + wc - 1)\n\t\t\t{\n\t\t\t\t*p++ = ' ';\n\t\t\t}\n\t\t}\n\t\twhile (p - lines[count] < maxWidth)\n\t\t{\n\t\t\t*p++ = ' ';\n\t\t}\n\t\t*p++ = '\\0';\n\t\tcount++;\n\t}\n\t*returnSize = count;\n\treturn lines;\n}\nint main(int argc, char **argv)\n{\n\tif (argc <= 2)\n\t{\n\t\tfprintf(stderr, \"Usage: ./test maxsize words...\\n\");\n\t\texit(-1);\n\t}\n\tint i, count;\n\tchar **lines = fullJustify(argv + 2, argc - 2, atoi(argv[1]), &count);\n\tfor (i = 0; i < count; i++)\n\t{\n\t\tprintf(\"%s\\n\", lines[i]);\n\t}\n\treturn 0;\n}",
    "language": "cpp"
  },
  "node_id": "dailycode-b7c191c627974320b5a16024eb016358",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470798"
}