예제 #1
0
        public void Test()
        {
            ZAlgorithm algorithm = new ZAlgorithm("abcdaaabchoabcd");

            Assert.That(algorithm.Lcp[1] == 0);
            Assert.That(algorithm.Lcp[2] == 0);
            Assert.That(algorithm.Lcp[3] == 0);
            Assert.That(algorithm.Lcp[4] == 1);
            Assert.That(algorithm.Lcp[5] == 1);
            Assert.That(algorithm.Lcp[6] == 3);
            Assert.That(algorithm.Lcp[7] == 0);
            Assert.That(algorithm.Lcp[8] == 0);
            Assert.That(algorithm.Lcp[9] == 0);
            Assert.That(algorithm.Lcp[10] == 0);
            Assert.That(algorithm.Lcp[11] == 4);
            Assert.That(algorithm.Lcp[12] == 0);
            Assert.That(algorithm.Lcp[13] == 0);
            Assert.That(algorithm.Lcp[14] == 0);

            Assert.That(algorithm.Compare(0) == 0);
            Assert.That(algorithm.Compare(1) > 0);
            Assert.That(algorithm.Compare(2) > 0);
            Assert.That(algorithm.Compare(3) > 0);
            Assert.That(algorithm.Compare(4) < 0);
            Assert.That(algorithm.Compare(5) < 0);
            Assert.That(algorithm.Compare(6) > 0);
            Assert.That(algorithm.Compare(7) > 0);
            Assert.That(algorithm.Compare(8) > 0);
            Assert.That(algorithm.Compare(9) > 0);
            Assert.That(algorithm.Compare(10) > 0);
            Assert.That(algorithm.Compare(11) < 0);
            Assert.That(algorithm.Compare(12) > 0);
            Assert.That(algorithm.Compare(13) > 0);
            Assert.That(algorithm.Compare(14) > 0);
        }
예제 #2
0
        public void String_Z_Test()
        {
            var kmpAlgo = new ZAlgorithm();

            var index = kmpAlgo.Search("xabcabzabc", "abc");

            Assert.AreEqual(1, index);

            index = kmpAlgo.Search("abdcdaabxaabxcaabxaabxay", "aabxaabxcaabxaabxay");

            Assert.AreEqual(5, index);

            index = kmpAlgo.Search("aaaabaaaaaaa", "aaaa");

            Assert.AreEqual(0, index);

            index = kmpAlgo.Search("abcabababdefgabcd", "fga");

            Assert.AreEqual(11, index);

            index = kmpAlgo.Search("abxabcabcaby", "abcaby");

            Assert.AreEqual(6, index);

            index = kmpAlgo.Search("abxabcabcaby", "abx");

            Assert.AreEqual(0, index);
        }
예제 #3
0
        public int StrStr(string haystack, string needle)
        {
            if (string.IsNullOrWhiteSpace(needle))
            {
                return(0);
            }

            var indices = ZAlgorithm.FindPattern(haystack, needle);

            return(indices.Length == 0 ? -1 : indices[0]);
        }
예제 #4
0
        public static ZFunction FromString(string text, ZAlgorithm algorithm)
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            switch (algorithm)
            {
            case ZAlgorithm.Naive:
                return(FromStringNaive(text));

            case ZAlgorithm.Linear:
                return(FromStringLinear(text));

            default:
                throw new NotImplementedException($"Algorithm {algorithm} is not implemented");
            }
        }
예제 #5
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            _ = inputStream.ReadInt();
            var s = inputStream.ReadLine();

            var max = 0;

            for (int start = 0; start < s.Length; start++)
            {
                var zAlgo = ZAlgorithm.SearchAll(s.AsSpan().Slice(start));

                for (int i = 0; i < zAlgo.Length; i++)
                {
                    max = Math.Max(max, Math.Min(i, zAlgo[i]));
                }
            }

            yield return(max);
        }
        public void ZAlgorithmSumomoTest(string input, params int[] expected)
        {
            var z = ZAlgorithm.SearchAll(input);

            Assert.Equal(expected, z);
        }
예제 #7
0
        public void Test1()
        {
            ZAlgorithm algorithm = new ZAlgorithm("-----");

            Assert.That(algorithm.Compare(2) < 0);
        }