【发布时间】:2019-02-06 23:49:00
【问题描述】:
我正在阅读来自 geeksforgeeks 的“将不同的事件计数为子序列问题”。我想了解“其他”的情况。在最后一个字符匹配的情况下,为什么S+没有最后一个字符的结果没有S和T的最后两个字符?
Input : S = banana, T = ban
Output : 3
T appears in S as below three subsequences.
[ban], [ba n], [b an]
// Returns count of subsequences of S that match T
// m is length of T and n is length of S
subsequenceCount(S, T, n, m)
// 一个空字符串是所有的子序列。 1) 如果 T 的长度为 0,则返回 1。
// 否则没有字符串可以是空 S 的序列。 2) 否则,如果 S 为空,则返回 0。
3) 否则,如果 S 和 T 的最后一个字符不匹配, 删除 S 的最后一个字符并重复剩余 return subsequenceCount(S, T, n-1, m)
4) Else(最后一个字符匹配),结果为 sum 两个计数。
// Remove last character of S and recur.
a) subsequenceCount(S, T, n-1, m) +
// Remove last characters of S and T, and recur.
b) subsequenceCount(S, T, n-1, m-1)
【问题讨论】:
标签: algorithm data-structures dynamic-programming