How to study rgss3 elegantly (1) Suffix automation implemented by ruby

Source: Internet
Author: User

* I really don't know Ruby *




#encoding: utf-8
# ====================================================== =============================
# ■ Suffix_Automaton
# ------------------------------------------------- -----------------------------
# Suffix automaton.
# ====================================================== ===============================

class Suffix_Automaton
  # ------------------------------------------------- -------------------------
  # ● Define instance variables
  # ------------------------------------------------- -------------------------
  attr_reader: total # number of different substrings in current SAM
  attr_reader: root # root node of SAM
  # ====================================================== =============================
  # ■ State
  # ------------------------------------------------- -----------------------------
  # Suffix automata status node.
  # ================================================= =============================
  class State
    # ------------------------------------------------- -------------------------
    # ● Define instance variables
    # ------------------------------------------------- -------------------------
    attr_accessor: par # parent
    attr_accessor: go # go
    attr_accessor: val # val
    # ------------------------------------------------- -------------------------
    # ● Initialization status node
    # ------------------------------------------------- -------------------------
    def init (val = 0)
      @par = nil
      @go = []
      @val = val
      for i in 0..26 do
        @go [i] = nil
      end
    end
    # ------------------------------------------------- -------------------------
    # ● Calculate the number of different substrings represented by the node
    # ------------------------------------------------- -------------------------
    def calc
      return 0 if @par == nil
      return @val-@ par.val
    end
  end
  # ------------------------------------------------- -------------------------
  # ● Initialize the suffix automaton
  # ------------------------------------------------- -------------------------
  def initSAM
    @total = 0
    @cur = 0
    @nodePool = []
    @root = newState
    @last = @root
  end
  # ------------------------------------------------- -------------------------
  # ● Create a new status node
  # ------------------------------------------------- -------------------------
  def newState (val = 0)
    @nodePool [@cur] = State.new
    @nodePool [@cur] .init (val)
    @cur + = 1
    return @nodePool [@ cur-1]
  end
  # ------------------------------------------------- -------------------------
  # ● Add characters
  # ------------------------------------------------- -------------------------
  def extend (w)
    p = @last
    np = newState (p.val + 1)
    while p! = nil and p.go [w] == nil do
      p.go [w] = np
      p = p.par
    end
    if p == nil
      np.par = @root
      @total + = np.calc # statistics
    else
      q = p.go [w]
      if p.val + 1 == q.val
        np.par = q
        @total + = np.calc # Statistics
      else
        nq = newState (p.val + 1)
        for i in 0..26 do
          nq.go [i] = q.go [i]
        end
        @total-= q.calc # Statistics
        nq.par = q.par
        q.par = nq
        np.par = nq
        @total + = q.calc + nq.calc + np.calc
        while p! = nil and p.go [w] == q do
          p.go [w] = nq
          p = p.par
        end
      end
    end
    @last = np
  end
end 






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.