Example #1
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            var testCases = scanner.NextInt();

            for (int i = 0; i < testCases; i++)
            {
                var line = scanner.Next();
                var result = 0;
                int stringNotCovered = line.Length;

                for (int j = 1; j < line.Length; j++)
                {
                    if (line.Substring(0, j).StartsWith(line.Substring(j, Math.Min(line.Length - j, j))))
                    {
                        //                        if (result == 0)
                        //                        {
                        //                            result = j;
                        //                            break;
                        //                        }
                        if (result == 0)
                        {
                            result = j;

                            int k = j;
                            while (k + result < line.Length && line.Substring(k, result) == line.Substring(0, j))
                            {
                                k += result;
                            }
                            stringNotCovered = line.Length - k - j;
                        }
                        else if (result != 0 && j % result == 0)
                        {
                            // ignore - we already handled it
                        }
                        else
                        {
                            // we already have a solution, just check if it covers more ground
                            // this one doesn't repeat itself
                            if (stringNotCovered > 0 && 2 * j > line.Length - stringNotCovered)
                            {
                                result = j;
                                stringNotCovered = 0;
                                break;
                            }
                        }
                    }
                }

                writer.Write(result);
                writer.Write("\n");
            }

            writer.Flush();
        }
Example #2
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            var input = scanner.NextInt();

            writer.Write(input*5);
            writer.Write("\n");
            writer.Flush();
        }
        public void OptimizedReader_Should_Return9_When_9()
        {
            // Arrange
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("9")))
            {
                var target = new OptimizedPositiveIntReader(ms);

                // Act
                var result = target.NextInt();

                // Assert
                Assert.That(result, Is.EqualTo(9));
            }
        }
Example #4
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            var numberOfTestCases = scanner.NextInt();

            for (var i = 0; i < numberOfTestCases; i++)
            {
                BigInteger x1 = scanner.NextInt();
                BigInteger y1 = scanner.NextInt();
                var operation = scanner.Next();
                BigInteger x2 = scanner.NextInt();
                BigInteger y2 = scanner.NextInt();

                Tuple<BigInteger, BigInteger> result;
                switch (operation)
                {
                    case "+":
                        result = Add(x1, y1, x2, y2);
                        break;
                    case "-":
                        // to substract is like to add but with the oposite sign
                        result = Add(x1, y1, -x2, y2);
                        break;
                    case "*":
                        result = Multiply(x1, y1, x2, y2);
                        break;
                    case "/":
                        // to divide is like to multiple by the oposite
                        result = Multiply(x1, y1, y2, x2);
                        break;
                    default:
                        throw new InvalidOperationException("no operation for:" + operation);
                }
                writer.Write(result.Item1);
                writer.Write(" / ");
                writer.Write(result.Item2);
                writer.Write("\n");
            }

            writer.Flush();
        }
Example #5
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            scanner = new LineReader(stdin);
            BufferedStdoutWriter writer = new BufferedStdoutWriter(stdout);

            var numberOfTestCases = scanner.NextInt();

            for (int i = 0; i < numberOfTestCases; i++)
            {

                var result = Solve(scanner);
                writer.Write(result);
                writer.Write("\n");
            }

            writer.Flush();
        }
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            // radius
            float r;
            // marked
            int m;
            // circle
            int c;

            while (true)
            {
                r = scanner.NextFloat();
                m = scanner.NextInt();
                c = scanner.NextInt();

                if (m == 0 && c == 0)
                    break;

                var circleSize = Math.PI * r * r;

                var estimatedSize = (c * 2 * r * 2 * r) / (float)m;

                writer.Write(circleSize.ToString(CultureInfo.InvariantCulture));
                //                writer.Write(circleSize.ToString(CultureInfo.InvariantCulture));
                writer.Write(" ");
                writer.Write(estimatedSize.ToString(CultureInfo.InvariantCulture));
                //                writer.Write(estimatedSize.ToString(CultureInfo.InvariantCulture));
                writer.Write("\n");
            }

            writer.Flush();
        }
Example #7
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            // players
            var n = scanner.NextInt();

            // connections
            var m = scanner.NextInt();

            Dictionary<int, HashSet<int>> connections = new Dictionary<int, HashSet<int>>();

            for (int i = 0; i < m; i++)
            {
                var a = scanner.NextInt();
                var b = scanner.NextInt();

                if (!connections.ContainsKey(a))
                {
                    connections.Add(a, new HashSet<int>());
                }
                connections[a].Add(b);

                if (!connections.ContainsKey(b))
                {
                    connections.Add(b, new HashSet<int>());
                }
                connections[b].Add(a);
            }

            // early exit
            if (connections.Count != n)
            {
                writer.Write("Impossible\n");
                writer.Flush();
                return;
            }

            var root = connections.OrderBy(c => c.Value.Count).First().Key;

            var solution = TraverseTree(root, new Tuple<HashSet<int>, HashSet<int>>(new HashSet<int>(), new HashSet<int>()), connections);

            if (solution.Item1.Count == n)
            {
                foreach (var node in solution.Item1)
                {
                    writer.Write(node);
                    writer.Write("\n");
                }
            }
            else
            {
                writer.Write("Impossible\n");
            }

            writer.Flush();
        }
Example #8
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            var n = scanner.NextInt();
            var m = scanner.NextInt();

            if (n == 1)
            {
                writer.Write(1);
                writer.Write("\n");
                writer.Flush();
                return;
            }

            var meetings = new Dictionary<int, HashSet<int>>(n);

            for (int i = 0; i < m; i++)
            {
                int a = scanner.NextInt();
                int b = scanner.NextInt();

                HashSet<int> forA;
                if (meetings.ContainsKey(a))
                {
                    forA = meetings[a];

                    if (!forA.Contains(b))
                        forA.Add(b);
                }
                else
                {
                    forA = new HashSet<int> { b };
                    meetings.Add(a, forA);
                }

                HashSet<int> forB;
                if (meetings.ContainsKey(b))
                {
                    forB = meetings[b];

                    if (!forB.Contains(a))
                        forB.Add(a);
                }
                else
                {
                    forB = new HashSet<int> { a };
                    meetings.Add(b, forB);
                }
            }

            var result = 0;

            foreach (var meeting in meetings.OrderByDescending(g => g.Value.Count))
            {
                // we know that for key all connections are there
                var everyoneBumped = new HashSet<int> { meeting.Key };
                Debug.WriteLine("Checking " + meeting.Key + " with: " + string.Join(", ", meeting.Value));

                for (int j = 0; j < meeting.Value.Count; j++)
                {
                    var personA = meeting.Value.ElementAt(j);

                    for (int i = j + 1; i < meeting.Value.Count; i++)
                    {
                        var personB = meeting.Value.ElementAt(i);
                        //Debug.WriteLine("Looking for {0} =? {1}", personA, personB);
                        if (!meetings[personA].Contains(personB))
                        {
                            //Debug.WriteLine("{0} <-> {1} not found", personA, personB);
                            personA = -1;
                            break;
                        }
                    }

                    if (personA > 0)
                        everyoneBumped.Add(personA);
                }

                Debug.WriteLine("Found group: " + string.Join(", ", everyoneBumped));
                result = Math.Max(everyoneBumped.Count, result);
            }

            writer.Write(result);
            writer.Write("\n");
            writer.Flush();
        }
Example #9
0
        public static void Solve(Stream stdin, Stream stdout)
        {
            IScanner scanner = new OptimizedPositiveIntReader(stdin);
            // uncomment when you need more advanced reader
            // scanner = new Scanner(stdin);
            // scanner = new LineReader(stdin);
            var writer = new BufferedStdoutWriter(stdout);

            var n = scanner.NextInt();

            var a = new int[n];
            var b = new int[n];

            for (var i = 0; i < n; i++)
            {
                a[i] = scanner.NextInt();
            }

            for (var i = 0; i < n; i++)
            {
                b[i] = scanner.NextInt();
            }

            var result = SolutionCompacting(a, b);

            writer.Write(result);
            writer.Write("\n");
            writer.Flush();
        }
        public void OptimizedReader_Should_ThrowException_When_StreamEmpty()
        {
            // Arrange
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("")))
            {
                var target = new OptimizedPositiveIntReader(ms);

                // Act
                target.NextInt();
            }
        }