using System.Collections.Generic; using System; public class Solution { enum EnvironmentType { None=0, Montain, Valley} public static void Main(string[] args) { IInput input; if (args.Length > 0) { input = new StringInput(System.IO.File.ReadAllLines(args[0])); } else { input = new ConsoleInput(); } var v = input.ReadLine(); int stepsCount = int.Parse(v); var stepsStr = input.ReadLine(); int level = 0; EnvironmentType type = EnvironmentType.None; int mountainCount = 0; int valleyCount = 0; for (int i = 0; i < stepsCount; i++) { if (stepsStr[i] == 'U') { level++; } else { level--; } if (level > 0) { type = EnvironmentType.Montain; } else if (level < 0) { type = EnvironmentType.Valley; } else { if (type == EnvironmentType.Montain) { mountainCount++; } else if (type == EnvironmentType.Valley) { valleyCount++; } type = EnvironmentType.None; } } Console.WriteLine(valleyCount); } public interface IInput { string ReadLine(); } public class ConsoleInput : IInput { public string ReadLine() { return Console.ReadLine(); } } public class StringInput : IInput { IEnumerable _data; IEnumerator _dataEnumerator; public StringInput (IEnumerable data) { _data = data; _dataEnumerator = _data.GetEnumerator(); } public string ReadLine() { _dataEnumerator.MoveNext(); var retVal = _dataEnumerator.Current; return retVal; } } }