SHARE
TWEET

SimpleTextEditor

a guest May 19th, 2019 64 in 149 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Advanced.StackAndQueues;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayDeque;
  7.  
  8. public class SimpleTextEditor {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         String emptyText = "";
  12.  
  13.         int N = Integer.parseInt(reader.readLine());
  14.         StringBuffer bd = new StringBuffer();
  15.         ArrayDeque<StringBuffer> queue = new ArrayDeque<>();
  16.         queue.push(new StringBuffer());
  17.         while (N-- > 0) {
  18.             String[] data = reader.readLine().split("\\s+");
  19.             switch (data[0]) {
  20.                 case "1":
  21.                     bd.append(data[1]);
  22.                     queue.push(new StringBuffer().append(bd));
  23.                     break;
  24.                 case "2":
  25.                     int index = Integer.parseInt(data[1]);
  26.                     if (index >= 0 && index <= bd.length()) {
  27.                         bd.delete(bd.length() - Integer.parseInt(data[1]), bd.length());
  28.                         queue.push(new StringBuffer().append(bd));
  29.                     }
  30. //                    System.out.println(bd);
  31.                     break;
  32.                 case "3":
  33.                     if (Integer.parseInt(data[1]) > 0 && Integer.parseInt(data[1]) <= bd.length()) {
  34.                         System.out.println(bd.charAt(Integer.parseInt(data[1]) - 1));
  35.                     }
  36.                     break;
  37.                 case "4":
  38.                     queue.pop();
  39.                     bd = new StringBuffer();
  40.                     bd.append(queue.peek());
  41.                     break;
  42.             }
  43.         }
  44.     }
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top