static void Main(string[] args)
        {
            BigInteger count = 0;

            string pattern = Console.ReadLine();
            string text = Console.ReadLine();

            int n = text.Length;
            int m = pattern.Length;

            Hash.ComputePowers(m);

            var prefix = pattern.Substring(0, 1);
            var suffix = pattern.Substring(1, m - 1);

            Hash hprefix = new Hash(prefix);
            Hash hsuffix = new Hash(suffix);

            Hash hPrefixWindowGeneral = new Hash(text.Substring(0, 1));
            Hash hSuffixWindowGeneral = new Hash(text.Substring(0, m - 1));

            var suffixes = new List<Hash>();
            var prefixes = new List<Hash>();
            var hWindowsPrefixes = new List<Hash>();
            var hWindowSuffixes = new List<Hash>();

            for (int i = 1; i <= m; i++)
            {
                suffixes.Add(new Hash(pattern.Substring(0, i)));
                prefixes.Add(new Hash(pattern.Substring(i, m - i)));
                hWindowsPrefixes.Add(new Hash(text.Substring(0, i)));
                hWindowSuffixes.Add(new Hash(text.Substring(0, m - i)));
            }

            for (int i = 1; i <= n; i++)
            {
                hPrefixWindow.Add(text[i + j - 1]);
                hPrefixWindow.Remove(text[i - 1], j);

                if (hprefix.Value == hPrefixWindow.Value)
                {
                    prefixCount++;
                }
            }

            for (int j = 1; j <= m; j++)
            {
                long prefixCount = 0;
                long suffixCount = 0;

                Hash hPrefixWindow = new Hash();
                hPrefixWindow.Value = hPrefixWindowGeneral.Value;
                Hash hSuffixWindow = new Hash();
                hSuffixWindow.Value = hSuffixWindowGeneral.Value;

                if (hprefix.Value == hPrefixWindow.Value)
                {
                    prefixCount++;
                }

                if (hsuffix.Value == hSuffixWindow.Value)
                {
                    suffixCount++;
                }

                //for prefix
                for (int i = 1; i <= n - j; i++)
                {
                    hPrefixWindow.Add(text[i + j - 1]);
                    hPrefixWindow.Remove(text[i - 1], j);

                    if (hprefix.Value == hPrefixWindow.Value)
                    {
                        prefixCount++;
                    }
                }

                //for suffix
                if (j != m)
                {
                    for (int i = 1; i <= n - (m - j); i++)
                    {
                        hSuffixWindow.Add(text[i + (m - j) - 1]);
                        hSuffixWindow.Remove(text[i - 1], (m - j));

                        if (hsuffix.Value == hSuffixWindow.Value)
                        {
                            suffixCount++;
                        }
                    }
                }
                else
                {
                    suffixCount = 1;
                }

                count += (prefixCount * suffixCount);

                // change prefix and suffix hash
                if (m != j)
                {
                    hprefix.Add(pattern[j]);
                    hsuffix.Remove(pattern[j], m - j - 1);
                    hPrefixWindowGeneral.Add(text[j]);
                    hSuffixWindowGeneral.Remove(text[j], m - j - 1);
                }
            }

            Console.WriteLine(count);
        }
Example #2
0
        static void Main()
        {
            var reader = new StreamReader("../../input/1.txt");
            Console.SetIn(reader);

            string pattern = Console.ReadLine();
            string text = Console.ReadLine();

            int n = text.Length;
            int m = pattern.Length;

            prefixes = new ulong[m];
            suffixes = new ulong[m];

            Hash.ComputePowers(m);
            Hash hpattern = new Hash(string.Empty);
            Hash suffPattern = new Hash(string.Empty);

            for (int subLen = 1; subLen <= m; subLen++)
            {
                hpattern.AddToBack(pattern[subLen - 1]);
                suffPattern.AddToFront(pattern[m - subLen], subLen);
                Hash hwindow = new Hash(text.Substring(0, subLen));

                if (hpattern.Value == hwindow.Value)
                {
                    prefixes[m - subLen - 1]++;
                }

                if (suffPattern.Value == hwindow.Value)
                {
                    suffixes[m - subLen - 1]++;
                }

                for (int i = 0; i < n - m; i++)
                {
                    hwindow.AddToBack(text[i + subLen]);
                    hwindow.Remove(text[i], subLen);

                    if (hpattern.Value == hwindow.Value)
                    {
                        prefixes[subLen - 1]++;
                    }

                    if (suffPattern.Value == hwindow.Value)
                    {
                        suffixes[subLen - 1]++;
                    }
                }
            }

            BigInteger result = 0;
            for (int i = 0; i < m - 1; i++)
            {
                result += prefixes[i] * suffixes[i];
            }

            result += prefixes[m - 1];

            Console.WriteLine(result);
        }