把一个波诡云谲的序列成甚至包裹? [英] Dividing a lumpy sequence into even parcels?

查看:112
本文介绍了把一个波诡云谲的序列成甚至包裹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想分成若干大致相等大小的包裹,同时保持的包裹(和包裹本身)的内容排序顺序项目的块状序列。在阳光下,因为没有什么是新的,我猜我只是缺少合适的名称问题。我的最终目标是一个Python实现的算法,但我至少需要一推在正确的方向。

I have a "lumpy" sequence of items that I want to divide into a certain number of roughly equal-sized parcels while maintaining the sort order of the contents of the parcels (and the parcels themselves). Since nothing is new under the sun, I'm guessing I'm just lacking the proper name for the problem. My end goal is a python implementation of the algorithm, but I at least need a push in the right direction.

现在的问题是,我有一个文本,分为不同长度的部分,我想将其划分成一系列公平的读数。当然读数的排序必须,保持相同。

The problem is that I have a text, divided into sections of varying length, and I want to divide it up into a series of equitable readings. The ordering of the readings must, of course, remain the same.

对于一些细节,我有2519段。最长的是1,876字,最短的为7个字。的平均长度为305字,并且平均长度为242

For some specifics, I have 2,519 sections. The longest is 1,876 words, the shortest is 7 words. The average length is 305 words, and the median length is 242.

推荐答案

我不知道,如果它完全解决了您的程序,但在麻省理工学院开放式课程<一href="http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/lecture-videos/"相对=nofollow>算法导论,他们使用动态规划求解线的最佳分裂,使文本是很好填写沿页面(自动换行'),类似于乳胶一样。参见第17分钟打入该影片

I am not sure if it exactly solves your program, but in the MIT OCW course Introduction to Algorithms, they use dynamic programming for solving the optimal splitting of lines so that the text is nicely filled out along a page ('word-wrapping'), similar to what Latex does. See 17 minutes into this video.

这个算法会给给出了一些函数,定义了基于多么丑陋分线是一个点球保证最佳的分裂。在讲座中,他们定义这个uglyness功能(页宽 - actual_linewidth)^ 3 ,但你可以定义自己的功能。该算法多于或少于尝试各种不同的分裂的可能性(在一个巧妙的方法),并从中选择最佳的之一。用于DP的主要要求是,这个问题可以被划分为子节目,如对于 N 的话可以根据previous解决方案描述为 N-1,N-2,... 的话。这些类型的DP算法通常为O(n ^ 2)为O(n ^ 3),所以绝对不NP难。

This algorithm will give the guaranteed optimal splitting given some function that defines a penalty based on how ugly the line-splitting is. In the lecture, they define this uglyness function as (pagewidth - actual_linewidth)^3, but you could define your own function. The algorithm than more or less tries all the different splitting possibilities (in a smart way) and chooses the optimal one. The main requirement for DP is that the problem can be divided into sub-programs, e.g. that the solution for n words can be described based on previous solutions for n-1, n-2, ... words. These type of DP algorithms are typically O(n^2) or O(n^3), so definitely not NP hard.

我强烈建议看,如果你有兴趣的基本算法,教师是伟大的全系列讲座。

I highly recommend watching the whole lecture series if you are interested in basic algorithms, the teachers are great.

这篇关于把一个波诡云谲的序列成甚至包裹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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