public static long[] PreComputeHashes(
            string T,
            int P,
            long p,
            long x
            )
        {
            int textLength = T.Length;

            long[] hashes = new long[textLength - P + 1];
            hashes[T.Length - P] = HashingWithChain.PolyHash(
                T, start: textLength - P, count: P, p: p, x: x);
            long y = 1;

            for (int i = 1; i <= P; i++)
            {
                y = (y * x) % p;
            }

            for (int i = textLength - P - 1; i >= 0; i--)
            {
                hashes[i] = (((x * hashes[i + 1]) + T[i] - (y * T[i + P])) % p + p) % p;
            }

            return(hashes);
        }
Exemple #2
0
        static void Main(string[] args)
        {
            HashingWithChain a = new HashingWithChain("TD2");

            a.Solve(5, new string[] { "add test", "add test", "find test", "check 0",
                                      "check 1", "check 2", "check 3", "check 4",
                                      "del test", "del test", "find test", "find Test", "add Test", "find Test" });
        }
Exemple #3
0
        public long[] Solve(string pattern, string text)
        {
            List <long> occurrences = new List <long>();
            long        hash        = HashingWithChain.PolyHash(pattern, 0, pattern.Length);
            var         textHashes  = PreComputeHashes(text, pattern.Length);

            for (long i = 0; i < textHashes.Length; i++)
            {
                if (hash == textHashes[i] && pattern == text.Substring((int)i, pattern.Length))
                {
                    occurrences.Add(i);
                }
            }
            return(occurrences.ToArray());
        }
        public static long[] PreComputeHashes(
            string T,
            int len,
            long p = BigPrimeNumber,
            long x = ChosenX)
        {
            long xp = HashingWithChain.Pow(x, len, p);

            long[] result = new long[T.Length - len + 1];
            result[T.Length - len] = HashingWithChain.PolyHash(T, T.Length - len, len, (int)p, p, x);
            for (int i = T.Length - len - 1; i >= 0; i--)
            {
                result[i] = ((x * result[i + 1] + T[i] - T[i + len] * xp % p) % p + p) % p;
            }
            return(result);
        }
        public static long[] PreComputeHashes(
            string T,
            int P,
            long p = BigPrimeNumber,
            long x = ChosenX)
        {
            long[] preComHashes = new long[T.Length - P + 1];
            preComHashes[T.Length - P] = HashingWithChain.PolyHash(T, T.Length - P, P, p, p, x);
            long y = HashingWithChain.Power(x, P);

            for (int i = T.Length - P - 1; i >= 0; i--)
            {
                preComHashes[i] = ((x * preComHashes[i + 1] + T[i] - y * T[i + P] % p) % p + p) % p;
            }
            return(preComHashes);
        }
        public long[] Solve(string pattern, string text)
        {
            List <long> occurrences = new List <long>();

            long[] preComputeHashes = PreComputeHashes(text, pattern.Length);
            long   patternHash      = HashingWithChain.PolyHash(pattern, 0, pattern.Length);

            for (int i = 0; i <= text.Length - pattern.Length; i++)
            {
                if (preComputeHashes[i] != patternHash)
                {
                    continue;
                }
                else if (pattern == text.Substring(i, pattern.Length))
                {
                    occurrences.Add(i);
                }
            }
            return(occurrences.ToArray());
        }
        public long[] Solve(string pattern, string text)
        {
            Random      r           = new Random();
            long        x           = r.Next(1, (int)BigPrimeNumber - 1);
            List <long> occurrences = new List <long>();

            long pHash = HashingWithChain.PolyHash(pattern, 0, pattern.Length, x: x);

            long[] tHashes = PreComputeHashes(text, pattern.Length, BigPrimeNumber, x);

            for (int i = 0; i <= text.Length - pattern.Length; i++)
            {
                if (pHash == tHashes[i])
                {
                    if (pattern.Equals(text.Substring(i, pattern.Length)))
                    {
                        occurrences.Add(i);
                    }
                }
            }

            return(occurrences.ToArray());
        }