求最小长度RLE

2024-01-26

经典的 RLE 算法通过使用数字来表示数字后面的字符在文本中该位置出现的次数来压缩数据。例如:

AAABBAAABBCECE => 3A2B3A2B1C1E1C1E

然而,在上面的示例中,该方法会导致压缩文本使用更多空间。更好的想法是使用数字来表示次数子串给定文本中出现以下数字。例如:

AAABBAAABBCECE => 2AAABB2CE(“AAABB”两次,然后“CE”两次)。

Now, my question is: how could I implement an efficient algorithm that finds out the minimum number of characters in an optimal RLE using this method? Brute force methods exist, but I need something faster (at most O(length2)). Perhaps we can use dynamic programming?


It can be done in quadratic cubic quadratic time via dynamic programming.

下面是一些 Python 代码:

import sys
import numpy as np

bignum = 10000

S = sys.argv[1] #'AAABBAAABBCECE'                                                                                                                              
N = len(S)

# length of longest substring match bet s[i:] and s[j:]                                                                                                        
maxmatch = np.zeros( (N+1,N+1), dtype=int)

for i in xrange(N-1,-1,-1):
  for j in xrange(i+1,N):
    if S[i] == S[j]:
      maxmatch[i,j] = maxmatch[i+1,j+1]+1

# P[n,k] = cost of encoding first n characters given that last k are a block                                                                                   
P = np.zeros( (N+1,N+1),dtype=int ) + bignum
# Q[n] = cost of encoding first n characters                                                                                                                   
Q = np.zeros(N+1, dtype=int) + bignum

# base case: no cost for empty string                                                                                                                          
P[0,0]=0
Q[0]=0

for n in xrange(1,N+1):
  for k in xrange(1,n+1):
    if n-2*k >= 0:
#     s1, s2 = S[n-k:n], S[n-2*k:n-k]                                                                                                                          
#     if s1 == s2:                                                                                                                                             
      if maxmatch[n-2*k,n-k] >=k:
        # Here we are incrementing the count: C x_1...x_k -> C+1 x_1...x_k                                                                                     
        P[n,k] = min(P[n,k], P[n-k,k])
        print 'P[%d,%d] = %d' % (n,k,P[n,k])
    # Here we are starting a new block: 1 x_1...x_k                                                                                                            
    P[n,k] = min(P[n,k], Q[n-k] + 1 + k)
    print 'P[%d,%d] = %d' % (n,k,P[n,k])
  for k in xrange(1,n+1):
    Q[n] = min(Q[n], P[n,k])

  print

print Q[N]

您可以通过记住一路上的选择来重建实际的编码。

我遗漏了一个小问题,那就是如果 C 很大,我们可能需要使用额外的字节来保存 C+1。如果您使用 32 位整数,则在该算法的运行时间可行的任何上下文中都不会出现这种情况。如果您有时使用较短的整数来节省空间,那么您将不得不考虑一下,并且可能会根据最新 C 的大小向表中添加另一个维度。理论上,这可能会添加一个 log(N) 因子,但是我认为这在实践中不会很明显。

编辑:为了@Moron的利益,这里是带有更多打印语句的相同代码,以便您可以更轻松地了解算法的想法:

import sys
import numpy as np

bignum = 10000

S = sys.argv[1] #'AAABBAAABBCECE'                                                                                                                              
N = len(S)

# length of longest substring match bet s[i:] and s[j:]                                                                                                        
maxmatch = np.zeros( (N+1,N+1), dtype=int)

for i in xrange(N-1,-1,-1):
  for j in xrange(i+1,N):
    if S[i] == S[j]:
      maxmatch[i,j] = maxmatch[i+1,j+1]+1

# P[n,k] = cost of encoding first n characters given that last k are a block                                                                                   
P = np.zeros( (N+1,N+1),dtype=int ) + bignum
# Q[n] = cost of encoding first n characters                                                                                                                   
Q = np.zeros(N+1, dtype=int) + bignum

# base case: no cost for empty string                                                                                                                          
P[0,0]=0
Q[0]=0

for n in xrange(1,N+1):
  for k in xrange(1,n+1):
    if n-2*k >= 0:
#     s1, s2 = S[n-k:n], S[n-2*k:n-k]                                                                                                                          
#     if s1 == s2:                                                                                                                                             
      if maxmatch[n-2*k,n-k] >=k:
        # Here we are incrementing the count: C x_1...x_k -> C+1 x_1...x_k                                                                                     
        P[n,k] = min(P[n,k], P[n-k,k])
        print "P[%d,%d] = %d\t I can encode first %d characters of S in only %d characters if I use my solution for P[%d,%d] with %s's count incremented" % (n\
,k,P[n,k],n,P[n-k,k],n-k,k,S[n-k:n])
    # Here we are starting a new block: 1 x_1...x_k                                                                                                            
    P[n,k] = min(P[n,k], Q[n-k] + 1 + k)
    print 'P[%d,%d] = %d\t I can encode first %d characters of S in only %d characters if I use my solution for Q[%d] with a new block 1%s' % (n,k,P[n,k],n,Q[\
n-k]+1+k,n-k,S[n-k:n])
  for k in xrange(1,n+1):
    Q[n] = min(Q[n], P[n,k])

  print
  print 'Q[%d] = %d\t I can encode first %d characters of S in only %d characters!' % (n,Q[n],n,Q[n])
  print


print Q[N]

ABCDABCDABCDBCD 上的输出的最后几行如下所示:

Q[13] = 7        I can encode first 13 characters of S in only 7 characters!

P[14,1] = 9      I can encode first 14 characters of S in only 9 characters if I use my solution for Q[13] with a new block 1C
P[14,2] = 8      I can encode first 14 characters of S in only 8 characters if I use my solution for Q[12] with a new block 1BC
P[14,3] = 13     I can encode first 14 characters of S in only 13 characters if I use my solution for Q[11] with a new block 1DBC
P[14,4] = 13     I can encode first 14 characters of S in only 13 characters if I use my solution for Q[10] with a new block 1CDBC
P[14,5] = 13     I can encode first 14 characters of S in only 13 characters if I use my solution for Q[9] with a new block 1BCDBC
P[14,6] = 12     I can encode first 14 characters of S in only 12 characters if I use my solution for Q[8] with a new block 1ABCDBC
P[14,7] = 16     I can encode first 14 characters of S in only 16 characters if I use my solution for Q[7] with a new block 1DABCDBC
P[14,8] = 16     I can encode first 14 characters of S in only 16 characters if I use my solution for Q[6] with a new block 1CDABCDBC
P[14,9] = 16     I can encode first 14 characters of S in only 16 characters if I use my solution for Q[5] with a new block 1BCDABCDBC
P[14,10] = 16    I can encode first 14 characters of S in only 16 characters if I use my solution for Q[4] with a new block 1ABCDABCDBC
P[14,11] = 16    I can encode first 14 characters of S in only 16 characters if I use my solution for Q[3] with a new block 1DABCDABCDBC
P[14,12] = 16    I can encode first 14 characters of S in only 16 characters if I use my solution for Q[2] with a new block 1CDABCDABCDBC
P[14,13] = 16    I can encode first 14 characters of S in only 16 characters if I use my solution for Q[1] with a new block 1BCDABCDABCDBC
P[14,14] = 15    I can encode first 14 characters of S in only 15 characters if I use my solution for Q[0] with a new block 1ABCDABCDABCDBC

Q[14] = 8        I can encode first 14 characters of S in only 8 characters!

P[15,1] = 10     I can encode first 15 characters of S in only 10 characters if I use my solution for Q[14] with a new block 1D
P[15,2] = 10     I can encode first 15 characters of S in only 10 characters if I use my solution for Q[13] with a new block 1CD
P[15,3] = 11     I can encode first 15 characters of S in only 11 characters if I use my solution for P[12,3] with BCD's count incremented
P[15,3] = 9      I can encode first 15 characters of S in only 9 characters if I use my solution for Q[12] with a new block 1BCD
P[15,4] = 14     I can encode first 15 characters of S in only 14 characters if I use my solution for Q[11] with a new block 1DBCD
P[15,5] = 14     I can encode first 15 characters of S in only 14 characters if I use my solution for Q[10] with a new block 1CDBCD
P[15,6] = 14     I can encode first 15 characters of S in only 14 characters if I use my solution for Q[9] with a new block 1BCDBCD
P[15,7] = 13     I can encode first 15 characters of S in only 13 characters if I use my solution for Q[8] with a new block 1ABCDBCD
P[15,8] = 17     I can encode first 15 characters of S in only 17 characters if I use my solution for Q[7] with a new block 1DABCDBCD
P[15,9] = 17     I can encode first 15 characters of S in only 17 characters if I use my solution for Q[6] with a new block 1CDABCDBCD
P[15,10] = 17    I can encode first 15 characters of S in only 17 characters if I use my solution for Q[5] with a new block 1BCDABCDBCD
P[15,11] = 17    I can encode first 15 characters of S in only 17 characters if I use my solution for Q[4] with a new block 1ABCDABCDBCD
P[15,12] = 17    I can encode first 15 characters of S in only 17 characters if I use my solution for Q[3] with a new block 1DABCDABCDBCD
P[15,13] = 17    I can encode first 15 characters of S in only 17 characters if I use my solution for Q[2] with a new block 1CDABCDABCDBCD
P[15,14] = 17    I can encode first 15 characters of S in only 17 characters if I use my solution for Q[1] with a new block 1BCDABCDABCDBCD
P[15,15] = 16    I can encode first 15 characters of S in only 16 characters if I use my solution for Q[0] with a new block 1ABCDABCDABCDBCD

Q[15] = 9        I can encode first 15 characters of S in only 9 characters!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

求最小长度RLE 的相关文章

  • 如何在javascript中计算日出和日落?

    我正在使用appcelerator titan开发一个IOS应用程序 我想让我的应用程序在日出和日落时向用户发送本地通知 解决这个问题的一个好工具是使用 YQL 的雅虎天气 但是 雅虎天气仅供非商业用途 我正在尝试找到一个javascrip
  • Javascript树遍历算法

    我需要帮助以深度优先的方式遍历树结构 我无法想出一个算法来正确地做到这一点 我的输入是这样的 A B C 1 2 a b c d 输出应采用以下形式 A 1 a A 1 b A 1 c A 1 d A 2 a A 2 b A 2 c A 2
  • 先增后减的最长子序列

    我正在尝试解决以下问题 元素值先减小后增大的序列称为V序列 在有效的 V 序列中 递减臂中应至少有一个元素 递增臂中至少应有一个元素 例如 5 3 1 9 17 23 是一个有效的 V 序列 在递减臂中具有两个元素 即 5 和 3 在递增臂
  • 链表分区函数及反转结果

    我编写了这个 F 函数来将列表分区到某个点并且不再进一步 很像之间的交叉takeWhile and partition let partitionWhile c l let rec aux accl accr match accr with
  • 找到三角测量时覆盖另一个点的最近 3 个点的算法

    想象一张画布 周围随机分布着一堆点 现在选择其中一点 您如何找到距离它最近的 3 个点 这样如果您画一个连接这些点的三角形 它将覆盖所选点 澄清 我所说的 最近 是指到该点的最小距离总和 这主要是出于好奇 我认为 如果一个点未知 但周围的点
  • 两个未排序小数组的交集算法

    我正在寻找一种在非常特定的条件下对两个小型未排序数组进行交集的算法 数组项的类型只是整数或类整数类型 在相当长的时间内 大约 30 40 一个或两个数组可能为空 数组通常非常小 通常为 1 3 个项目 我预计不会超过 10 个 交集函数会被
  • D3.js 对力导向图使用什么算法?

    我有兴趣确切地知道 D3 使用什么算法来实现库中的力导向图功能 读过科布罗夫的总结 http www cs brown edu rt gdhandbook chapters force directed pdf力导向图的历史让我有点困惑 不
  • 验证是否存在唯一字符串的组合

    class Details String name String age String email String location 1 如果有详细信息列表 如下所示List
  • Lamport 的 Paxos 中的矛盾做了简单的论文

    阶段 2 a 如果提议者收到大多数接受者对其准备请求 编号为 n 的响应 则它向每个接受者发送一个接受请求 以获取编号为 n 且值为 v 的提案 其中 v 是响应中编号最高的提案的值 或者如果响应未报告任何提案 则为任意值 正如论文中提到的
  • 按顺时针顺序对四个点排序

    数组中的四个 2D 点 我需要按顺时针顺序对它们进行排序 我认为只需一次交换操作就可以完成 但我还没有能够正式放下这一点 编辑 在我的例子中 这四个点是凸多边形 编辑 这四个点是凸多边形的顶点 它们不必按顺序排列 如果你想从更数学的角度来看
  • 计算排列中“反转”的数量

    设 A 为一个大小的数组N 我们称之为几个索引 i j 一个 逆 如果i lt j and A i gt A j 我需要找到一种接收大小数组的算法N 具有唯一的数字 并返回时间的倒数数O n log n 您可以使用归并排序 http en
  • 将曲线图案与图像边缘匹配

    我有一个要搜索沿其边缘的曲线的目标图像和一个包含该曲线的模板图像 我需要实现的是在目标图像中找到模板图像中的曲线的最佳匹配 并根据分数来判断是否匹配 这还包括曲线的旋转和大小调整 目标图像可以是 Canny Edge 检测器的输出 如果这能
  • 如何编写一个简单的版本控制系统?

    我想做一个简单的版本控制系统 但我不知道如何构建我的数据和代码 这是一个简短的例子 用户登录 User has two options when uploading a file 提交新文件 提交文件的新版本 用户应该能够看到树 版本不同
  • 如何在 Perl 中生成数组的所有排列?

    生成所有内容的最佳 优雅 简单 高效 方式是什么 n perl 中数组的排列 例如 如果我有一个数组 arr 0 1 2 我想输出所有排列 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0 它可能应该是一个返回迭代器的
  • 转置矩阵存储在一维数组中,无需使用额外的内存[重复]

    这个问题在这里已经有答案了 可能的重复 矩阵的就地转置 https stackoverflow com questions 9227747 in place transposition of a matrix 最近参加了技术笔试 通过以下问
  • Welzl 算法的迭代版本

    我正在使用 Welzl 算法来查找点云的最小外接圆 2d 或最小外接球体 3d 不幸的是 该算法具有非常高的递归深度 即输入点数 这个算法有迭代版本吗 我找不到任何并且不知道如何将递归更改为循环 我发现了一些迭代的最小包围圆 球算法 但它们
  • 我想知道像tineye.com这样的反向图像搜索服务是如何工作的......?

    像 TinEye 这样的反向图像搜索引擎如何工作 我的意思是进行图像搜索需要哪些参数 不知道 TinEye 是否使用这个 但是SURF http en wikipedia org wiki SURF是用于此目的的常用算法 在这里您可以看到一
  • 如何找到权重为 1、0、-1 且成本精确为 0 的多维路径

    我得到了一个有向图 其中有 n 个节点和边 向量的权重 每个向量的长度为 m 为数字 1 0 1 我想找到从一个节点到另一个节点 我们可以多次访问节点 的任何路径 或者说这样的路径不存在 使其权重之和等于仅由零组成的向量 我正在考虑暴力回溯
  • 如何决定权重?

    对于我的工作 我需要某种具有以下输入和输出的算法 输入 一组日期 过去的日期 输出 一组权重 每个给定日期一个权重 所有权重的总和 1 基本思想是 距离今天日期最近的日期应该获得最高的权重 第二个最接近的日期将获得第二高的权重 依此类推 有
  • 排序数组最快的搜索方法是什么?

    正在回答另一个问题 https stackoverflow com questions 4752028 whats wrong with this interpolation search implementation 4752042 47

随机推荐