public static void Main() { #if DEBUG Console.SetIn(new System.IO.StreamReader("../../input.txt")); #endif var output = new StringBuilder(); var line = Console.ReadLine(); while (line != "End") { var command = new Command(line); switch (command.Name) { case "Append": AppendCommand(command, output); break; case "Insert": InsertCommand(command, output); break; case "Find": FindCommand(command, output); break; case "Serve": ServeCommand(command, output); break; default: break; } line = Console.ReadLine(); } Console.Write(output); }
private static void AppendCommand(Command command, StringBuilder output) { name.Add(command.Param); if (!findNames.ContainsKey(command.Param)) { findNames.Add(command.Param, 0); } findNames[command.Param]++; output.AppendLine("OK"); }
private static void InsertCommand(Command command, StringBuilder output) { if (command.Index >= 0 && name.Count >= command.Index) { name.Insert(command.Index, command.Param); if (!findNames.ContainsKey(command.Param)) { findNames.Add(command.Param, 0); } findNames[command.Param]++; output.AppendLine("OK"); } else { output.AppendLine("Error"); } }
private static void ServeCommand(Command command, StringBuilder output) { int count = int.Parse(command.Param); if (count > name.Count) { output.AppendLine("Error"); } else { while (count > 0) { output.Append(name[0] + " "); if (findNames.ContainsKey(name[0])) { findNames[name[0]]--; } name.RemoveAt(0); count--; } output.Append(Environment.NewLine); } }
private static void FindCommand(Command command, StringBuilder output) { if (findNames.ContainsKey(command.Param)) { output.AppendLine(findNames[command.Param].ToString()); } else { output.AppendLine("0"); } }