Skip to main content
Rollback to Revision 6
Source Link
toolic
  • 6.1k
  • 4
  • 20
  • 73

We cannot use Rcpp - R base packages only.

For regexpr with a sliding window I have

KMP_version = function(s) {
  pattern = as.integer(strsplit(s, "")[[1]])
  m = length(pattern)
  
  compute_failure = function(pattern) {
    n = length(pattern)
    lps = integer(n)
    len = 0
    lps[1] = 0
    
    for (i in 2:n) {
      while (len > 0 && pattern[len + 1] != pattern[i]) {
        len = lps[len]
      }
      if (pattern[len + 1] == pattern[i]) {
        len = len + 1
      }
      lps[i] = len
    }
    return(lps)
  }
  
  get_digits = function(num) {
      if (num == 0) return(0)
      digits = integer(floor(log10(num)) + 1)
      idx = length(digits)
      while (num > 0) {
        digits[idx] = num %% 10
        num = num %/% 10
        idx = idx - 1
      }
      return(digits)
  }
  
  lps = compute_failure(pattern)
  
  idx = 0 # current matching idx in s
  pos = 0 # current pos in S, the infinite string
  n = 1   # current number
  
  repeat {
    digits = get_digits(n)
    
    for (digit in digits) {
      pos = pos + 1
      
      while (idx > 0 && digit != pattern[idx + 1]) {
        idx = lps[idx]
      }
      
      if (digit == pattern[idx + 1]) idx = idx + 1
      
      if (idx == m) return(pos - m + 1)
    }
    n = n + 1
  }
}
'''regexpr_version = function(s) {
bock_size  m = 10000nchar(s)
  buffer = ""          # Buffer to hold the sliding window
  pos = 0         # Position in the infinite string S
  n = 1                # Current number to append
  batch_size = 10000
  
  repeat {
# concatenate  10000 numbers one= atpaste0(n:(n a+ timebatch_size to- the1), collapse = "")
    search_str = paste0(buffer, numbers)
...    match = regexpr(s, search_str, fixed = TRUE)[1]
    
    # match found
    if no(match != -1) {
      start_pos = pos - nchar(buffer) + match 
 found shrink the   return(start_pos)
    }
    
    pos = pos + nchar(numbers)
    buffer = substr(search_str, andnchar(search_str) increment- currentm number+ and2, positionnchar(search_str))
    n = n + batch_size
  }
}

R base packages only.

For regexpr with a sliding window I have

'''
bock_size = 10000
buffer = ""

repeat {
# concatenate  10000 numbers one at a time to the buffer
...
# if no match found shrink the buffer, and increment current number and position
}

We cannot use Rcpp - R base packages only.

KMP_version = function(s) {
  pattern = as.integer(strsplit(s, "")[[1]])
  m = length(pattern)
  
  compute_failure = function(pattern) {
    n = length(pattern)
    lps = integer(n)
    len = 0
    lps[1] = 0
    
    for (i in 2:n) {
      while (len > 0 && pattern[len + 1] != pattern[i]) {
        len = lps[len]
      }
      if (pattern[len + 1] == pattern[i]) {
        len = len + 1
      }
      lps[i] = len
    }
    return(lps)
  }
  
  get_digits = function(num) {
      if (num == 0) return(0)
      digits = integer(floor(log10(num)) + 1)
      idx = length(digits)
      while (num > 0) {
        digits[idx] = num %% 10
        num = num %/% 10
        idx = idx - 1
      }
      return(digits)
  }
  
  lps = compute_failure(pattern)
  
  idx = 0 # current matching idx in s
  pos = 0 # current pos in S, the infinite string
  n = 1   # current number
  
  repeat {
    digits = get_digits(n)
    
    for (digit in digits) {
      pos = pos + 1
      
      while (idx > 0 && digit != pattern[idx + 1]) {
        idx = lps[idx]
      }
      
      if (digit == pattern[idx + 1]) idx = idx + 1
      
      if (idx == m) return(pos - m + 1)
    }
    n = n + 1
  }
}
regexpr_version = function(s) {
  m = nchar(s)
  buffer = ""          # Buffer to hold the sliding window
  pos = 0         # Position in the infinite string S
  n = 1                # Current number to append
  batch_size = 10000
  
  repeat {
    numbers = paste0(n:(n + batch_size - 1), collapse = "")
    search_str = paste0(buffer, numbers)
    match = regexpr(s, search_str, fixed = TRUE)[1]
    
    # match found
    if (match != -1) {
      start_pos = pos - nchar(buffer) + match 
      return(start_pos)
    }
    
    pos = pos + nchar(numbers)
    buffer = substr(search_str, nchar(search_str) - m + 2, nchar(search_str))
    n = n + batch_size
  }
}
deleted 1720 characters in body
Source Link

We cannot use Rcpp - R base packages only.

KMP_version = function(s) {
  pattern = as.integer(strsplit(s, "")[[1]])
  m = length(pattern)
  
  compute_failure = function(pattern) {
    n = length(pattern)
    lps = integer(n)
    len = 0
    lps[1] = 0
    
    for (i in 2:n) {
      while (len > 0 && pattern[len + 1] != pattern[i]) {
        len = lps[len]
      }
      if (pattern[len + 1] == pattern[i]) {
        len = len + 1
      }
      lps[i] = len
    }
    return(lps)
  }
  
  get_digits = function(num) {
      if (num == 0) return(0)
      digits = integer(floor(log10(num)) + 1)
      idx = length(digits)
      while (num > 0) {
        digits[idx] = num %% 10
        num = num %/% 10
        idx = idx - 1
      }
      return(digits)
  }
  
  lps = compute_failure(pattern)
  
  idx = 0 # current matching idx in s
  pos = 0 # current pos in S, the infinite string
  n = 1   # current number
  
  repeat {
    digits = get_digits(n)
    
    for (digit in digits) {
      pos = pos + 1
      
      while (idx > 0 && digit != pattern[idx + 1]) {
        idx = lps[idx]
      }
      
      if (digit == pattern[idx + 1]) idx = idx + 1
      
      if (idx == m) return(pos - m + 1)
    }
    n = n + 1
  }
}

For regexpr with a sliding window I have

regexpr_version = function(s) {'''
  mbock_size = nchar(s)10000
  buffer = ""          # Buffer to hold the sliding window
  pos = 0         # Position in the infinite string S
  n = 1                # Current number to append
  batch_size = 10000
  
  repeat {
# concatenate  10000 numbers = paste0(n:(n + batch_size - 1), collapse = "")
one at a time search_strto =the paste0(buffer, numbers)
    match = regexpr(s, search_str, fixed = TRUE)[1]
    ...
    # match found
    if (match != -1) {
      start_pos = pos - nchar(buffer) +no match
      return(start_pos)
    }
    
    pos = pos + nchar(numbers)
 found shrink the buffer = substr(search_str, nchar(search_str) - m + 2, nchar(search_str))
    n =and nincrement +current batch_size
number and }position
}

We cannot use Rcpp - R base packages only.

KMP_version = function(s) {
  pattern = as.integer(strsplit(s, "")[[1]])
  m = length(pattern)
  
  compute_failure = function(pattern) {
    n = length(pattern)
    lps = integer(n)
    len = 0
    lps[1] = 0
    
    for (i in 2:n) {
      while (len > 0 && pattern[len + 1] != pattern[i]) {
        len = lps[len]
      }
      if (pattern[len + 1] == pattern[i]) {
        len = len + 1
      }
      lps[i] = len
    }
    return(lps)
  }
  
  get_digits = function(num) {
      if (num == 0) return(0)
      digits = integer(floor(log10(num)) + 1)
      idx = length(digits)
      while (num > 0) {
        digits[idx] = num %% 10
        num = num %/% 10
        idx = idx - 1
      }
      return(digits)
  }
  
  lps = compute_failure(pattern)
  
  idx = 0 # current matching idx in s
  pos = 0 # current pos in S, the infinite string
  n = 1   # current number
  
  repeat {
    digits = get_digits(n)
    
    for (digit in digits) {
      pos = pos + 1
      
      while (idx > 0 && digit != pattern[idx + 1]) {
        idx = lps[idx]
      }
      
      if (digit == pattern[idx + 1]) idx = idx + 1
      
      if (idx == m) return(pos - m + 1)
    }
    n = n + 1
  }
}
regexpr_version = function(s) {
  m = nchar(s)
  buffer = ""          # Buffer to hold the sliding window
  pos = 0         # Position in the infinite string S
  n = 1                # Current number to append
  batch_size = 10000
  
  repeat {
    numbers = paste0(n:(n + batch_size - 1), collapse = "")
    search_str = paste0(buffer, numbers)
    match = regexpr(s, search_str, fixed = TRUE)[1]
    
    # match found
    if (match != -1) {
      start_pos = pos - nchar(buffer) + match
      return(start_pos)
    }
    
    pos = pos + nchar(numbers)
    buffer = substr(search_str, nchar(search_str) - m + 2, nchar(search_str))
    n = n + batch_size
  }
}

R base packages only.

For regexpr with a sliding window I have

'''
bock_size = 10000
buffer = ""

repeat {
# concatenate  10000 numbers one at a time to the buffer
...
# if no match found shrink the buffer, and increment current number and position
}
Became Hot Network Question
Spelling and grammar; edited tags
Source Link
Toby Speight
  • 81.7k
  • 14
  • 101
  • 308

How to write efficient pattern Pattern search algorithm for infinite integer string in R

I have tried the KMP algorithm. It runs fine on small input, make did poorbut poorly on input such as str = "667788999". It takes forever to finish.
I also tried to use regexpr with a sliding window,window; the above test case will still cost 20s. Our programprograms are tested on unknownhidden test cases. And even using the second approach, my program still timedouttimed out for 1 test case.

You can notWe cannot use Rcpp, - R base packagepackages only.

How to write efficient pattern search algorithm for infinite integer string in R

I have tried the KMP algorithm. It runs fine on small input, make did poor on input such as str = "667788999". It takes forever to finish.
I also tried to use regexpr with a sliding window, the above test case will still cost 20s. Our program are tested on unknown test cases. And even using the second approach, my program still timedout for 1 test case.

You can not use Rcpp, R base package only.

Pattern search algorithm for infinite integer string in R

I have tried the KMP algorithm. It runs fine on small input, but poorly on input such as str = "667788999". It takes forever to finish.
I also tried to use regexpr with a sliding window; the above test case will still cost 20s. Our programs are tested on hidden test cases. And even using the second approach, my program still timed out for 1 test case.

We cannot use Rcpp - R base packages only.

layout
Source Link
greybeard
  • 6.6k
  • 3
  • 20
  • 52
Loading
added 3 characters in body
Source Link
Loading
added 13 characters in body
Source Link
Loading
added 33 characters in body
Source Link
Loading
Source Link
Loading