如何使我的程序更快地进行模式匹配?

时间:2014-03-22 05:25:19

标签: r vector time-complexity

我不喜欢我编码的方式,我认为应该有比这更快的算法,或者R可能有设施来解决这个问题。

    four.in.a.row = function(player, v, debug=TRUE) {
  if (debug) {
    cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
  }
  for (i in 1:(length(v)-4))
    if (v[i]==player)
      if (v[i]==v[i+1])
        if (v[i+1]==v[i+2])
          if (v[i+2]==v[i+3])

          return(TRUE)
  # ...
  return(FALSE) 
}
#   four.in.a.row("X", c("O","X","X","X","X","O"))
# is TRUE, while
#   four.in.a.row("O", c("O","X","X","X","X","O"))
# is FALSE.

请告诉我如何提高复杂性。还能告诉我当前算法的复杂程度吗?

当我使用以下内容时,我收到错误:

four.in.a.row = function(player, v, debug=TRUE) {
  if (debug) {
    cat(sep="", "four.in.a.row(player=", player, ", v=", v, ")\n")
  }


 for (i in 1:(length(v)-4))
  { 
    count=1
    if (v[i]==player)
      for (j in i+1:i+3)
        if (v[i]==v[j])
          count=count+1
    if (count==4) 
      return(TRUE)
  }
  # ...
  return(FALSE) # correct this return() statement
}

Error in if (v[i] == v[j]) count = count + 1 : 
  missing value where TRUE/FALSE needed

1 个答案:

答案 0 :(得分:2)

您可以使用rle

four.in.a.row <- function(player, v) {
  with(rle(v), any(lengths >= 4 & values == player))
}

这是一个更加神秘的方法,应该更快:

four.in.a.row <- function(player, v) {
  x <- player == v
  in.a.row <- seq_along(x) - cummax(seq_along(x) * !x)
  any(in.a.row >= 4)
}

最后,您可以使用正则表达式:

four.in.a.row = function(player, v) {
  x <- paste(v, collapse = "")
  pattern <- paste(rep(player, 4), collapse = "")
  grepl(pattern, x)
}

您必须对数据进行测试,以确定哪种方法更快。如果足够快,我会采用rle方法来简洁。