コード例 #1
0
        /// <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);
        }