Syntax Colorizer

permlink
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class KSubstring
{
    class TreapNode
    {
        public static TreapNode Insert(TreapNode root, long val)
        {
            TreapNode left, right;
            Split(root, val, out left, out right);
            return Merge(Merge(left, new TreapNode(val)), right);
        }

        public TreapNode(long val)
        {
            this.val = val;
            pri = rnd.Next();
        }

        public static long FindLessEqDiff(TreapNode root, long x)
        {
            if (root == null)
                return long.MaxValue;
            if (root.val == x)
                return 0;
            if (root.val < x)
            {
                long z = FindLessEqDiff(root.right, x);
                return z == long.MaxValue ? root.val - x : z;
            }
            else
                return FindLessEqDiff(root.left, x);
        }

        public static long FindGreaterEqDiff(TreapNode root, long x)
        {
            if (root == null)
                return long.MaxValue;
            if (root.val == x)
                return 0;
            if (root.val < x)
                return FindGreaterEqDiff(root.right, x);
            else
            {
                long z = FindGreaterEqDiff(root.left, x);
                return z == long.MaxValue ? root.val - x : z;
            }
        }

        private static void Split(TreapNode root, long val, out TreapNode left, out TreapNode right)
        {
            if (root == null)
            {
                left = null;
                right = null;
            }
            else if (root.val < val)
            {
                Split(root.right, val, out root.right, out right);
                left = root;
            }
            else
            {
                Split(root.left, val, out left, out root.left);
                right = root;
            }
        }

        private static TreapNode Merge(TreapNode left, TreapNode right)
        {
            if (left == null)
                return right;
            else if (right == null)
                return left;
            else if (left.pri >= right.pri)
            {
                left.right = Merge(left.right, right);
                return left;
            }
            else
            {
                right.left = Merge(left, right.left);
                return right;
            }
        }

        private TreapNode left, right;
        private long val;
        private int pri;
        private static Random rnd = new Random();
    }

    public int[] maxSubstring(int A0, int X, int Y, int M, int n)
    {
        long[] A = new long[n];
        A[0] = A0;
        for (int i = 1; i < n; i++)
            A[i] = (A[i - 1] * X + Y) % M;

        long minDiff = long.MaxValue;
        int maxLen = 0;
        for (int len = 1; 2 * len <= n; len++)
        {
            long[] sums = new long[n];
            for (int i = 0; i < n; i++)
            {
                if (i < len)
                    sums[len - 1] += A[i];
                else
                    sums[i] = sums[i - 1] + (A[i] - A[i - len]);
            }

            TreapNode root = null;
            for (int fst = len - 1, snd = 2 * len - 1; snd < n; fst++, snd++)
            {
                root = TreapNode.Insert(root, sums[fst]);
                long target = sums[snd];
                long nval = Math.Min(
                    Math.Abs(TreapNode.FindLessEqDiff(root, target)),
                    Math.Abs(TreapNode.FindGreaterEqDiff(root, target))
                );
                if (nval <= minDiff)
                {
                    maxLen = len;
                    minDiff = nval;
                }
            }
        }

        return new int[] { maxLen, (int) minDiff };
    }
}

For you blog

For web page

About | Send us feedback