public static IEnumerable <long> Solve(string[] commands) { var level = new SequenceLevel(); var solutions = new Stack <long>(); for (var t = 0; t < commands.Length - 1; t++) { var command = commands[t + 1]; var sol = 0L; if (command[0] == '+') { var x = long.Parse(command.Substring(2)); //var shortcut = level.Seq.Count > 0 && x == level.Seq[(int)solutions.Peek()] ? (long?)(solutions.Peek() + 1) : null; level.AddLetter(x); sol = Solve_Reduce_Rec(level); solutions.Push(sol); //Console.WriteLine(); //var lll = level; //while (lll.Seq.Count > 0) //{ // Console.WriteLine(lll.Seq.Join() + " -> " + CalcMinLen(lll.lengths)); // lll = lll.DeeperLevel; //} } else if (level.Seq.Count > 0) { level.RemoveLastLetter(); solutions.Pop(); sol = solutions.Count > 0 ? solutions.Peek() : 0; } yield return(Solve_Reduce_Rec(level)); } }
public void AddLetter(long letter) { if (Seq.Count == 0) { a = letter; Seq.Add(letter); } else { if (letter == a) { if (Tag.Count == 0) { var minLen = CalcMinLen(lengths); if (minLen != minLength && DeeperLevel.Seq.Count > 0) { var deepA = DeeperLevel.Seq[0]; var deepSeq = DeeperLevel.Seq.Take(DeeperLevel.Seq.Count - DeeperLevel.Tag.Count).ToList(); var deepTag = DeeperLevel.Tag; var letterMap = DeeperLevel.LettersMap; deeperLevel = new SequenceLevel { Level = Level + 1, LettersMap = letterMap }; foreach (var list in GroupByA(deepSeq, deepA)) { if (list[0] == deepA) { for (var i = 0; i < list.Count; i++) { deeperLevel.AddLetter(deepA); } } else { foreach (var l in list) { deeperLevel.AddLetter(l); } } } foreach (var l in deepTag) { deeperLevel.AddLetter(l); } minLength = minLen; } } var b = LettersMap.GetMap(Tag); lengths.Add(Tag.Count); Tag.Clear(); DeeperLevel.AddLetter(b); } else { Tag.Add(letter); } Seq.Add(letter); } }