Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add finite state automaton to KMP #199

Open
hockyy opened this issue Aug 30, 2021 · 0 comments
Open

Add finite state automaton to KMP #199

hockyy opened this issue Aug 30, 2021 · 0 comments

Comments

@hockyy
Copy link

hockyy commented Aug 30, 2021

// Needs failure function of kmp to work,
// automaton builds a string recognizer
// Input : String `s`, First starting character `ch`, character size `charSize` (numbers of available characters)
// Output : A Finite state machine, initial at 0, accepting state at sz(s).
// Time complexity, Space Complexity: O(charSize * sz(S)) 
vvi automaton(string s, char ch, int charSize) {
  s += '$'; vi ff = pi(s);
  vvi aut(sz(s), vi(charSize));
  rep(i, 0, sz(s)) {
    rep(c, 0, charSize) {
      aut[i][c] = ((i > 0 && ch + c != s[i]) ? aut[ff[i - 1]][c] : (i + (ch + c == s[i])));
    }
  }
  return aut;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant