/// <summary> /// Recursively search the children of this <see cref="Node{T}"/> to find the key /// <para>Returns 'null' if key does not have a value</para> /// </summary> internal Node <T> Search(string key) { if (key.Length == 1) { return(children[alphabet.ToIndex(key[0])]); } if (children[alphabet.ToIndex(key[0])] != null) { return(children[alphabet.ToIndex(key[0])].Search(key.Substring(1))); } return(null); }
public int Search(string s) { int N = s.Length; int M = pat.Length; int skip; for (int i = 0; i <= N - M; i += skip) { skip = 0; for (int j = M - 1; j >= 0; j--) { if (pat[j] != s[i + j]) { skip = j - right[alphabet.ToIndex(s[i + j])]; if (skip < 1) { skip = 1; } break; } } if (skip == 0) { return(i); // found. } } return(-1); // not found }
public SubstringSearchKMP(string pat, IAlphabet alphabet) { this.pat = pat; this.alphabet = alphabet; int M = pat.Length; int R = alphabet.R; dfa = new int[R, M]; dfa[alphabet.ToIndex(pat[0]), 0] = 1; for (int X = 0, j = 1; j < M; j++) { for (int r = 0; r < R; r++) { dfa[r, j] = dfa[r, X]; } dfa[alphabet.ToIndex(pat[j]), j] = j + 1; X = dfa[alphabet.ToIndex(pat[j]), X]; } }
public int Search(string s) { int i; int j; int N = s.Length; int M = pat.Length; for (i = 0, j = 0; i < N && j < M; i++) { j = dfa[alphabet.ToIndex(s[i]), j]; } return(j == M ? i - M : -1); }
public void Sort(string[] a, int w) { string[] aux = new string[a.Length]; for (int d = w - 1; d >= 0; d--) { // Sort by key-indexed counting on dth char // Compute frequency count int[] count = new int[alphabet.R + 1]; for (int i = 0; i < a.Length; i++) { int charIndex = alphabet.ToIndex(a[i][d]); count[charIndex + 1]++; } // Transform counts to indices for (int r = 0; r < alphabet.R; r++) { count[r + 1] += count[r]; } // Distribute for (int i = 0; i < a.Length; i++) { int charIndex = alphabet.ToIndex(a[i][d]); aux[count[charIndex]++] = a[i]; } // Copy back for (int i = 0; i < a.Length; i++) { a[i] = aux[i]; } } }
public SubstringSearchBoyerMoore(string pat, IAlphabet alphabet) { this.pat = pat; this.alphabet = alphabet; int M = pat.Length; int R = alphabet.R; right = new int[R]; for (int r = 0; r < R; r++) { right[r] = -1; } for (int j = 0; j < M; j++) { right[alphabet.ToIndex(pat[j])] = j; } }