You are viewing a single comment's thread. Return to all comments →
C#
using System; using System.Collections.Generic; using System.IO; using System.Text; class Editor { private StringBuilder _sb = new StringBuilder(); private Stack<Action> _history = new Stack<Action>(); public void Append(string s) { int start = _sb.Length; int len = s.Length; _history.Push(() => _sb.Remove(start, len)); _sb.Append(s); } public void DeleteLast(int n) { string last = Substring(_sb.Length - n); _history.Push(() => _sb.Append(last)); _sb.Remove(_sb.Length - n, n); } private string Substring(int start) { char[] buffer = new char[_sb.Length - start]; for (int i = 0, j = start; j < _sb.Length; i++, j++) buffer[i] = _sb[j]; return new string(buffer); } public char GetChar(int n) =>_sb[n - 1]; public void Undo() { if (_history.Count > 0) _history.Pop()(); } } class Solution { static void Main(String[] args) { var editor = new Editor(); int q = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < q; i++) { string[] parts = Console.ReadLine().Split(' '); int cmd = Convert.ToInt32(parts[0]); int k; switch (cmd) { case 1: editor.Append(parts[1]); break; case 2: k = Convert.ToInt32(parts[1]); editor.DeleteLast(k); break; case 3: k = Convert.ToInt32(parts[1]); char c = editor.GetChar(k); Console.WriteLine(c); break; case 4: editor.Undo(); break; } } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
C#