- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不知道在这里问这个问题是否合适,如果不合适,请见谅。
我得到了一个序列 ALPHA,例如:
A B D Z A B X
我得到了 ALPHA 的子序列列表,例如:
A B D
B D
A B
D Z
A
B
D
Z
X
我搜索了一种算法,该算法找到不连续子序列的最小长度 重建 ALPHA,例如在我们的案例中:
{A B D} {Z} {A B} {X}
有什么想法吗?我猜有些东西已经存在了。
最佳答案
您可以将此问题转化为寻找图中的最小路径。
节点将对应于字符串的前缀,包括一个用于空字符串的前缀。如果存在一个允许的子序列,当附加到字符串前缀 A 时,结果是字符串前缀 B,则将存在从节点 A 到节点 B 的边。
问题现在转化为寻找图中从空字符串对应的节点开始,到整个输入字符串对应的节点结束的最小路径。
您现在可以应用例如 BFS(因为边具有统一成本)或 Dijkstra 算法来找到此路径。
下面的python代码是基于上述原理的实现:
def reconstruct(seq, subseqs):
n = len(seq)
d = dict()
for subseq in subseqs:
d[subseq] = True
# in this solution, the node with value v will correspond
# to the substring seq[0: v]. Thus node 0 corresponds to the empty string
# and node n corresponds to the entire string
# this will keep track of the predecessor for each node
predecessors = [-1] * (n + 1)
reached = [False] * (n + 1)
reached[0] = True
# initialize the queue and add the first node
# (the node corresponding to the empty string)
q = []
qstart = 0
q.append(0)
while True:
# test if we already found a solution
if reached[n]:
break
# test if the queue is empty
if qstart > len(q):
break
# poll the first value from the queue
v = q[qstart]
qstart += 1
# try appending a subsequence to the current node
for n2 in range (1, n - v + 1):
# the destination node was already added into the queue
if reached[v + n2]:
continue
if seq[v: (v + n2)] in d:
q.append(v + n2)
predecessors[v + n2] = v
reached[v + n2] = True
if not reached[n]:
return []
# reconstruct the path, starting from the last node
pos = n
solution = []
while pos > 0:
solution.append(seq[predecessors[pos]: pos])
pos = predecessors[pos]
solution.reverse()
return solution
print reconstruct("ABDZABX", ["ABD", "BD", "AB", "DZ", "A", "B", "D", "Z", "X"])
我没有太多使用 Python 的经验,这就是为什么我更喜欢坚持基础知识的主要原因(例如实现一个带有列表 + 开头索引的队列)。 p>
关于algorithm - 查找算法 : Reconstruct a sequence with the minimum length combination of disjointed subsequences chosen from a list of subsequences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43653446/
题目地址:https://leetcode.com/problems/reconstruct-itinerary/description/ 题目描述 Given a list of airline
题目地址:https://leetcode.com/problems/queue-reconstruction-by-height/#/descriptionopen in new window 题
我已经用两个相机的固有相机矩阵成功计算了旋转、平移。我还从左右摄像头获得了校正后的图像。现在,我想知道如何计算一个点的 3D 坐标,只是图像中的一个点。在这里,请看绿点。我看了一下方程,但它需要我不知
我正在使用 boost:serialization 将数据结构保存到文件中。实际数据是类和子类的指针 vector 。然而,被序列化的类的构造函数将另一个实例化类 Agent 作为参数,它是一个控制与
我正在使用 RESTEasy 来使用 REST 服务,我正在尝试使用 Twitter 的搜索 API。 所以我创建了这个界面: public interface SimpleClient { @G
题目地址:https://leetcode.com/problems/reconstruct-original-digits-from-english/description/ 题目描述: Giv
我有这些数据类型: data PointPlus = PointPlus { coords :: Point , velocity :: Vector } deriving (
我的目的是通过另一个 Hermes2 向 MSH (Hermes2 http://www.cecid.hku.hk/hermes.php ) 发送 ebxml 消息。 我正在发送相当简单的消息(没有附
我是 opencv 库的初学者。我已经在 Ubuntu 17.04 上安装了它,安装过程中的一切都很完美,一点错误都没有。我已经安装了 Opencv-master,构建了它,然后我下载了 opencv
我目前正在尝试从NERF模型重建网格,我注意到大多数NERF实现都提供了一个Python脚本,用于通过COLMAP从图像中提取相机姿势。我的理解是,这些脚本使用稀疏重建,或者至少使用COLMAP的特征
我正在将 ARKit 与 SceneKit 结合使用,并想让我的 3D 对象与 reconstructed scene 物理交互由带有 LiDAR 传感器的设备创建 (config.sceneReco
我正在回答下面的问题。 序列 [0, 1, ..., N] 被打乱了,你对它的顺序的唯一线索是一个数组,表示每个数字是大于还是小于最后一个。给定这些信息,重建一个与其一致的数组。 例如,给定 [Non
映射任意时髦的嵌套列表的最简单方法是什么expr到函数 unflatten以便 expr==unflatten@@Flatten@expr ? 动机:Compile只能处理完整的数组(我刚学到的东西—
typedef struct { union { uint32_t ss32; struct { unsigned int res
我刚开始学习 Rx,并尝试使用 SerialPort 从 GPS 设备实现“NMEA 句子阅读器”。事实上,它的 GPS 数据对这个问题来说不太重要,所以让我们澄清一下 NMEA 格式由线条组成,“$
If you want to keep your Neural Network architecture secret and still want to use it in an applic
If you want to keep your Neural Network architecture secret and still want to use it in an applic
我不知道在这里问这个问题是否合适,如果不合适,请见谅。 我得到了一个序列 ALPHA,例如: A B D Z A B X 我得到了 ALPHA 的子序列列表,例如: A B D B D A B D Z
我有这两个二叉树序列(不是 BSD): 顺序:3 2 1 4 5 7 6 后序:3 1 2 5 6 7 4 我们知道postOrder中的最后一个元素是根,所以我们把根定位在inOrder序列上,这样
我是一名优秀的程序员,十分优秀!