将文本均匀地分成一定的行数 [英] Break text evenly into certain number of lines

查看:85
本文介绍了将文本均匀地分成一定的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种线性时间算法(或Knuth& Plass的二次时间算法),用于将文本均匀地分成最大宽度的行.它使用SMAWK,平均"表示:
http://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness

在上面是否有一种算法或凹成本函数可以考虑我希望文本插入的行数,而不是最大行宽?

换句话说,我正在寻找一种换行(或段落形成或自动换行)算法,其中输入是所需的行数,而不是所需的行宽.

仅描述一种实际上不可用的方法:每个单词对之间有N个单词和N-1个空格,M是所需的行数(M <= N).在每个空格之后,最多可能会有一个(可能为零)换行符.现在,该算法将尝试将中断放置在每个可能的组合中,计算参差不齐"并返回最佳组合.如何快得多?

解决方案

通过将最大长度计算为的总长度,可以简单地将获得给定数量的行的问题减少为在最大长度后断开行的问题.字符串除以所需的行数.在许多情况下,由于实际的行长会小于最大长度,因此您可能需要从所需的行数中减去1.

There is a linear time algorithm (or quadratic time algorithm by Knuth & Plass) for breaking text evenly into lines of maximum width. It uses SMAWK and "evenly" means:
http://en.wikipedia.org/wiki/Word_wrap#Minimum_raggedness

Is there an algorithm or a concave cost function for algorithm above which would take into account the number of lines I would like the text break into, instead of the maximum line width?

In other words, I'm looking for a line breaking (or paragraph formation, or word wrapping) algorithm where the input is the desired number of lines, not the desired line width.

Just to describe a practically unusable approach: There are N words and N-1 spaces in-between each word pair, M is the desired number of lines (M <= N). After each space there might be at most one (possibly zero) line-break. Now, the algorithm would try to place the breaks in each possible combination, calculating the "raggedness" and return the best one. How to do it much faster?

解决方案

You could simply reduce the problem of achieving a given number of lines to the problem of breaking lines after a maximum length by calculating the maximum length as the total length of the string divided by the number of lines you want. As the actual length of a line is going to be less than the maximum length in many cases, you would probably need to subtract 1 from the number of lines you want.

这篇关于将文本均匀地分成一定的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆