-3

有人能告诉我什么是最长的平坦小节吗?

4

1 回答 1

2

给定一个数组 a[0:n) (n≧0),如果 ∀i,j:p≦i≦j<q,则它的子部分 a[p,q) (0≦p≦q≦n) 称为“平面” :a[i]=a[j]。

翻译成英语为“当且仅当序列中的所有元素彼此相等时,子序列才被认为是平坦的”。由于等式是传递的、自反的和对称的,你可以发现它:

pre: a is a sequence of symbols
     n is its length

last = null
bestAt = null
bestLen = -1
for each i in 0..n-1
  if a[i] != last
    thisAt = i
    thisLen = 1
    last=a[i]
    if thisLen > bestLen
      bestLen = thisLen
      bestAt = thisAt
  else
    thisLen++
  last=a[i]

post: a is not modified
      n is not modified
      bestAt holds the position of the first longest subsequence
      bestLen holds the length of the first longest subsequence
于 2012-11-19T10:13:19.693 回答