public static int Search(string pattern, string source, int startIndex) { char[] x = pattern.ToCharArray(), y = source.ToCharArray(startIndex, source.Length - startIndex); int j, init, ell, state, m = x.Length, n = y.Length; Graph aut; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); /* Preprocessing */ aut = Automata.NewSuffixAutomaton(2 * (m + 2), 2 * (m + 2) * 65536); BuildSuffixAutomaton(x, ref aut); init = Automata.GetInitial(aut); stopwatch.Stop(); preProcessTime = stopwatch.Elapsed.TotalMilliseconds; stopwatch.Restart(); /* Searching */ ell = 0; state = init; for (j = 0; j < n; ++j) { if (Automata.GetTarget(aut, state, y[j]) != -1) { ++ell; state = Automata.GetTarget(aut, state, y[j]); } else { while (state != init && Automata.GetTarget(aut, state, y[j]) == -1) { state = Automata.GetSuffixLink(aut, state); } if (Automata.GetTarget(aut, state, y[j]) != -1) { ell = Automata.GetLength(aut, state) + 1; state = Automata.GetTarget(aut, state, y[j]); } else { ell = 0; state = init; } } if (ell == m) { stopwatch.Stop(); searchTime = stopwatch.Elapsed.TotalMilliseconds; return(j - m + 1 + startIndex); } } stopwatch.Stop(); searchTime = stopwatch.Elapsed.TotalMilliseconds; return(-1); }
public static int Search(string pattern, string source, int startIndex) { char[] x = pattern.ToCharArray(), y = source.ToCharArray(startIndex, source.Length - startIndex); int i, j, shift, period, init, state, m = x.Length, n = y.Length; Graph aut; char[] xR; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); /* Preprocessing */ aut = Automata.NewSuffixAutomaton(2 * (m + 2), 2 * (m + 2) * 65536); xR = Reverse(x); BuildSuffixAutomaton(xR, ref aut); init = Automata.GetInitial(aut); period = m; stopwatch.Stop(); preProcessTime = stopwatch.Elapsed.TotalMilliseconds; stopwatch.Restart(); /* Searching */ j = 0; while (j <= n - m) { i = m - 1; state = init; shift = m; while (i + j >= 0 && Automata.GetTarget(aut, state, y[i + j]) != -1) { state = Automata.GetTarget(aut, state, y[i + j]); if (Automata.IsTerminal(aut, state)) { period = shift; shift = i; } --i; } if (i < 0) { stopwatch.Stop(); searchTime = stopwatch.Elapsed.TotalMilliseconds; return(j + startIndex); } j += shift; } stopwatch.Stop(); searchTime = stopwatch.Elapsed.TotalMilliseconds; return(-1); }
public static List <int> Search(string pattern, string source) { char[] x = pattern.ToCharArray(), y = source.ToCharArray(); int period, i, j, shift, u, periodOfU, disp, init, state, mu, m = x.Length, n = y.Length; int[] mpNext = new int[m + 1]; List <int> result = new List <int>(); char[] xR; Graph aut; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); /* Preprocessing */ aut = Automata.NewSuffixAutomaton(2 * (m + 2), 2 * (m + 2) * 65536); xR = Reverse(x); BuildSuffixAutomaton(xR, ref aut); init = Automata.GetInitial(aut); PreMp(x, ref mpNext); period = m - mpNext[m]; i = 0; shift = m; stopwatch.Stop(); preProcessTime = stopwatch.Elapsed.TotalMilliseconds; stopwatch.Restart(); /* Searching */ j = 0; while (j <= n - m) { i = m - 1; state = init; u = m - 1 - shift; periodOfU = (shift != m ? m - shift - mpNext[m - shift] : 0); shift = m; disp = 0; while (i > u && Automata.GetTarget(aut, state, y[i + j]) != -1) { disp += Automata.GetShift(aut, state, y[i + j]); state = Automata.GetTarget(aut, state, y[i + j]); if (Automata.IsTerminal(aut, state)) { shift = i; } --i; } if (i <= u) { if (disp == 0) { result.Add(j); shift = period; } else { mu = (u + 1) / 2; if (periodOfU <= mu) { u -= periodOfU; while (i > u && Automata.GetTarget(aut, state, y[i + j]) != -1) { disp += Automata.GetShift(aut, state, y[i + j]); state = Automata.GetTarget(aut, state, y[i + j]); if (Automata.IsTerminal(aut, state)) { shift = i; } --i; } if (i <= u) { shift = disp; } } else { u = u - mu - 1; while (i > u && Automata.GetTarget(aut, state, y[i + j]) != -1) { disp += Automata.GetShift(aut, state, y[i + j]); state = Automata.GetTarget(aut, state, y[i + j]); if (Automata.IsTerminal(aut, state)) { shift = i; } --i; } } } } j += shift; } stopwatch.Stop(); searchTime = stopwatch.Elapsed.TotalMilliseconds; return(result); }