Ejemplo n.º 1
0
        public TAnswer DP(TSample Sample)
        {
            SpecialStringAgainSample sample = Sample as SpecialStringAgainSample;

            String s      = sample.s;
            Int64  result = 0;

            Int64[] F = new Int64[s.Length + 1];
            F[0] = 1;
            F[1] = 1;
            Char    CurrentChar = s[0];
            Int64   cc          = 1;
            Boolean OneChar     = true;

            for (int i = 1; i < s.Length; i++)
            {
                F[i + 1] = F[i] + i + 1;
                if (s[i] == CurrentChar)
                {
                    cc++;
                }
                else
                {
                    OneChar = false;
                    int j = i + 1;
                    result += F[cc];

                    while (j < s.Length && s[j] == CurrentChar)
                    {
                        j++;
                    }

                    int cc2 = j - i - 1;
                    if (cc2 > 0)
                    {
                        result += Math.Min(cc, cc2);
                    }
                    CurrentChar = s[i];
                    cc          = 1;
                }
            }

            if (OneChar)
            {
                result = F[s.Length];
            }
            else
            {
                result += F[cc];
            }


            return(new SpecialStringAgainAnswer()
            {
                result = result
            });
        }
Ejemplo n.º 2
0
        public override void CreateSamples(System.IO.StreamReader reader)
        {
            reader.ReadLine();
            SpecialStringAgainSample sample = new SpecialStringAgainSample()
            {
                s = reader.ReadLine()
            };

            Samples.Add(sample);
        }