/// <summary> /// Wyznaczanie minimalnego szablonu /// </summary> /// <param name="text">Napis do namalowania</param> /// <param name="indexes">Tablica zawierająca indeksy "punktów przyłozenia" początku szablonu</param> /// <returns>Długość szablonu</returns> public static int MinPattern(string text, out int[] indexes) { int[] P = StringMatching.ComputeP(text); int textLength = text.Length; int shortestPatternLength = textLength; List <int> shortestPatternMatches = new List <int>() { 0 }; foreach (int prefixLength in P.Where(x => x > 0).Distinct().OrderByDescending(x => x)) { //if (shortestPatternLength < textLength && shortestPatternLength <= 2 * prefixLength) //{ // // Using "tip 2" // shortestPatternLength = prefixLength; // shortestPatternMatches = null; //} //else //{ // Check if the pattern matches string prefix = text.Substring(0, prefixLength); int[] prefixP = StringMatching.ComputeP(prefix); List <int> matches = StringMatching.KMP(text, prefix, prefixP); bool patternMatches = IsPattern(text, prefix, matches); if (patternMatches) { shortestPatternLength = prefixLength; shortestPatternMatches = matches; } //} } if (shortestPatternMatches == null) { string prefix = text.Substring(0, shortestPatternLength); shortestPatternMatches = StringMatching.KMP(text, prefix, StringMatching.ComputeP(prefix)); } indexes = shortestPatternMatches.ToArray(); return(shortestPatternLength); }
/// <summary> /// Badanie równoważności cyklicznej z wykorzystaniem algorytmu KMP /// </summary> /// <param name="text1">Pierwszy badany tekst</param> /// <param name="text2">Drugi badany tekst</param> /// <returns> /// Jeśli teksty nie są cyklicznie równoważne zwraca null /// Jeśli teksty są cyklicznie równoważne zwraca liczbę znaków, /// które trzeba przenieść z początku pierwszego tekstu na koniec aby uzyskać drugi tekst /// </returns> public static int?CyclicEquivalenceKMP(string text1, string text2) { int n = text1.Length; if (n != text2.Length) { return(null); } if (text1 == text2) { return(0); } int[] P = StringMatching.ComputeP(text1 + text2); if (P.Last() == 0) { return(null); } return(P.Last()); }