예제 #1
0
        public static void Main()
        {
            string pattern = Console.ReadLine();
            string text = Console.ReadLine();

            //if (pattern.Length > text.Length)
            //{
            //    Console.WriteLine(0);
            //    return;
            //}

            Hash hpattern = new Hash(pattern);
            Hash hwindow = new Hash(text.Substring(0, pattern.Length));

            List<int> matches = new List<int>();

            if (hpattern.Hash1 == hwindow.Hash1 && hpattern.Hash2 == hwindow.Hash2)
            {
                matches.Add(1);
            }

            for (int i = pattern.Length; i < text.Length; i++)
            {
                hwindow.Add(text[i], i);
                hwindow.Remove(text[i - pattern.Length], i - pattern.Length);

                if (hpattern.Hash1 == hwindow.Hash1 && hpattern.Hash2 == hwindow.Hash2)
                {
                    matches.Add(i - pattern.Length + 2);
                }
            }

            int sum = 0;
            for (int i = 0; i < matches.Count; i++)
            {
                sum += matches[i].ToString().Length;
            }

            var result = sum / matches.Count;

            Console.WriteLine("{0}", result);
            //Console.WriteLine("{0}\n{1}", matches.Count, string.Join(" ", matches));
        }
예제 #2
0
        static void Main(string[] args)
        {
            string pattern = Console.ReadLine();
            string text = Console.ReadLine();

            if (pattern.Length > text.Length)
            {
                Console.WriteLine(0);
                return;
            }

            Hash hpattern = new Hash(pattern, pattern.Length);
            Hash hwindow = new Hash(text.Substring(0, pattern.Length), text.Length);

            List<int> matches = new List<int>();

            if (hpattern.Hash1 == hwindow.Hash1 && hpattern.Hash2 == hwindow.Hash2)
            {
                matches.Add(1);
            }

            for (int i = pattern.Length; i < text.Length; i++)
            {
                hwindow.Add(text[i], i);
                hwindow.Remove(text[i - pattern.Length], i - pattern.Length);

                if (hpattern.Hash1 == hwindow.Hash1 && hpattern.Hash2 == hwindow.Hash2)
                {
                    matches.Add(i - pattern.Length + 2);
                }
            }

            Console.WriteLine("{0}\n{1}", matches.Count, string.Join(" ", matches));
        }
예제 #3
0
        public static void Main(string[] args)
        {
            string pattern = Console.ReadLine();
            string text = Console.ReadLine();

            if (pattern.Length > text.Length)
            {
                Console.WriteLine(0);

                return;
            }

            Hash patternHash = new Hash(pattern);
            Hash textHash = new Hash(text.Substring(0, pattern.Length));

            List<int> matches = new List<int>();

            if (patternHash.Hash1 == textHash.Hash1 && patternHash.Hash2 == textHash.Hash2)
            {
                matches.Add(1);
            }

            for (int i = pattern.Length; i < text.Length; i++)
            {
                textHash.Add(text[i], i);
                textHash.Remove(text[i - pattern.Length], i - pattern.Length);

                if (patternHash.Hash1 == textHash.Hash1 && patternHash.Hash2 == textHash.Hash2)
                {
                    matches.Add(i - pattern.Length + 2);
                }
            }

            Console.WriteLine(matches.Count);
            Console.WriteLine(string.Join(" ", matches));
        }