Пример #1
0
        private static void SearchPattern(string pat, string txt)
        {
            //var pattern = pat.ToCharArray();
            //var text = txt.ToCharArray();

            var rabinKarp1 = new RabinKarp(pat);
            var offset1    = rabinKarp1.Search(txt);



            // print results
            Console.WriteLine("text:    " + txt);

            Console.Write("pattern: ");
            for (var i = 0; i < offset1; i++)
            {
                Console.Write(" ");
            }
            Console.WriteLine(pat);

            //Console.Write("pattern: ");
            //for (var i = 0; i < offset2; i++)
            //    Console.Write(" ");
            Console.WriteLine(pat);
        }
Пример #2
0
        public void Test()
        {
            String text = "o fare thee well, poor devil of a Sub-Sub, whose commen- \n" + "tator I am. Thou belongest to that hopeless, sallow tribe \n"
                          + "which no wine of this world will ever warm ; and for whom \n" + "even Pale Sherry would be too rosy-strong ; but with whom \n"
                          + "one sometimes loves to sit, and feel poor-devilish, too ; and \n"
                          + "grow convivial upon tears ; and say to them bluntly with full \n" + "eyes and empty glasses, and in not altogether unpleasant \n"
                          + "sadness Give it up, Sub-Subs ! For by how much the more \n" + "pains ye take to please the world, by so much the more shall \n"
                          + "ye forever go thankless ! Would that I could clear out \n" + "Hampton Court and the Tuileries for ye ! But gulp down \n"
                          + "your tears and hie aloft to the royal-mast with your hearts ; \n" + "for your friends who have gone before are clearing out the \n"
                          + "seven-storied heavens, and making refugees of long-pampered \n" + "Gabriel, Michael, and Raphael, against your coming. Here \n"
                          + "ye strike but splintered hearts together there, ye shall \n" + "strike unsplinterable glasses! ";

            RabinKarp bm = new RabinKarp("the");

            print("found at " + bm.Search(text));
            Assert.NotEqual(-1, bm.Search(text));
        }
Пример #3
0
        public void String_RabinKarp_Test()
        {
            var algorithm = new RabinKarp();

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

            Assert.AreEqual(1, index);

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

            Assert.AreEqual(5, index);

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

            Assert.AreEqual(0, index);

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

            Assert.AreEqual(11, index);

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

            Assert.AreEqual(6, index);

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

            Assert.AreEqual(0, index);
        }