简体   繁体   English

根据单词之间的空格查找文本的宽度(JAVA)

[英]Finding the width of a text depending on spaces between words (JAVA)

I've been working on a problem of optimization, that requires me to find the width of a text that gives me the longest river. 我一直在研究优化问题,这需要我找到给我最长河流的文字宽度。 A river is defined as a sequence of spaces that are only separated by 1 or less characters in different lines. 河流被定义为一系列空间,它们在不同的行中仅由1个或更少的字符分隔。 For example, I have the following text: 例如,我有以下文字:

Here's the image, the red "x" means the rivers. 这是图像,红色“x”表示河流。

图片

-> A river is a sequence of spaces in different lines that are separated by one character to the left, to the right or none. - >河流是不同行中的一系列空间,由左边,右边或没有一个字符分隔。 We want the longest of them all. 我们想要最长的一切。

The text has to be aligned to the left but not to the right, and a word cannot be divided if it does not fit. 文本必须与左侧对齐,而不是右侧,如果不适合,则不能分割单词。

I've been trying to work with the length of each word, instead of all the sequence of characters in order to get a better execution, but I'm not being able to solve the problem in this way. 我一直在努力处理每个单词的长度,而不是所有字符序列,以便更好地执行,但我无法以这种方式解决问题。 I can only think of testing all the possibilities (the minimum width is the length of the longest word), and getting the better one, but I don't think that's efficient in time and in space. 我只能考虑测试所有可能性(最小宽度是最长单词的长度),并获得更好的可能性,但我不认为这在时间和空间上都是有效的。

I don't want you to solve the problem, I just want to know if you think it could be solved in another way. 我不想让你解决这个问题,我只是想知道你是否认为它可以用另一种方式解决。 Thanks! 谢谢!

EDIT: For example, given a text, I want to return the width and the length of the river. 编辑:例如,给定一个文本,我想返回河流的宽度和长度。 图片

Well, I see no point is saying YES, YOU CAN withoutsome hints, so here you have... 好吧,我认为没有必要说是的,你可以没有一些提示,所以在这里你有......

I believe you should start by defining what you know as rules deriving from the base question: 我相信你应该从定义你所知道的基本问题的规则开始:

Assumptions: 假设:

  1. There will not be at any location within the string two or more consecutive spaces, 字符串中的任何位置都不会有两个或多个连续的空格,
  2. there will be no word-wrapping and hence the minimal width will be determined by the length of the longest word, 没有自动换行,因此最小宽度将由最长单词的长度决定,
  3. Blanks at the end of a line are not to be considered as part of a river (I'm not sure this would be a correct rule as you can imagine situations where you have a 6 lines river and one line out of these 6 happens to finish earlier because the next word is too long). 一条线末端的空白不应被视为河流的一部分(我不确定这是一个正确的规则,因为你可以想象你有一条6线河的情况,而这6条线中的一条线恰好发生在早点完成,因为下一个词太长了)。

You would still need to find what is the length of the longest word but this only to set the minimal width to begin from. 你仍然需要找到最长单词的长度,但这只是为了设置从最小的宽度开始。

So, you do know that: 所以,你知道:

  1. You have a long string that need to be wrapped into at least three records, such that the third record has at least two words (you need this to enable the potential creation of a 3-lines river, which is the minimum, right?), 你有一个长字符串需要包含在至少三个记录中,这样第三个记录至少有两个单词(你需要这个来启用3行河的潜在创建,这是最小的,对吗?) ,
  2. As such, you can calculate the maximal length of any line, meaning, length of the whole string - length of the last two words - 1 (the 1 is the blank between the last two words) divided by 2 . 因此,您可以计算任何行的最大长度,即length of the whole string - length of the last two words - 11是最后两个单词之间的空白)除以2

With these assumptions and facts , you can simply loop deploying the text into a matrix (each cell holding one character) and examining, line by line, of you find rivers complying with the rules. 通过这些假设和事实 ,您可以简单地将文本循环部署到矩阵(每个单元格中包含一个字符)并逐行检查您是否发现符合规则的河流。

You cant make this alghoritm more effective as I do not see any pattern that you can use to "I dont have to try this line width, it does not give me better result than this line width". 你不能让这个alghoritm更有效,因为我没有看到你可以使用的任何模式“我不必尝试这个线宽,它不会给我比这个线宽更好的结果”。

So basically you have to try to find all solution for all line widths and take the best. 所以基本上你必须尝试找到所有线宽的所有解决方案,并采取最好的。 The only optimization is when you start from the smallest line widths to bigger ones and you find river longer than number of rows that will come - then you dont have to continue. 唯一的优化是当你从最小的线宽开始到更大的线宽时,你会发现河流的长度超过了行数 - 那么你就不必继续了。

For example - if you find longest river with 4 value and your line width reached 4 rows, you can stop it as you cant find longer river. 例如 - 如果您发现最长的河流有4个值且线宽达到4行,您可以停止它,因为您无法找到更长的河流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM