You are viewing a single comment's thread. Return to all comments →
Java O(1)
public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out)); int Q = Integer.parseInt(bufferedReader.readLine().trim()); StringBuilder S = new StringBuilder(); Stack<String> history = new Stack<>(); for (int i = 0; i < Q; i++) { String[] inputs = bufferedReader.readLine().split(" "); int type = Integer.parseInt(inputs[0]); switch (type) { case 1: // Append(W) history.push(S.toString()); S.append(inputs[1]); break; case 2: // Delete(k) int k = Integer.parseInt(inputs[1]); history.push(S.toString()); S.delete(S.length() - k, S.length()); break; case 3: // Print(k) int index = Integer.parseInt(inputs[1]) - 1; bufferedWriter.write(S.charAt(index)); bufferedWriter.newLine(); break; case 4: // Undo S = new StringBuilder(history.pop()); break; } } bufferedReader.close(); bufferedWriter.flush(); bufferedWriter.close(); }
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 →
Java O(1)