Problem Statement
append - Append string to the end of .
delete - Delete the last characters of .
print - Print the character of .
undo - Undo the last (not previously undone) operation of type or , reverting to the state it was in prior to that operation.
Input Format
The first line contains an integer, , denoting the number of operations.
Each line of the subsequent lines (where ) defines an operation to be performed. Each operation starts with a single integer, (where ), denoting a type of operation as defined in the Problem Statement above. If the operation requires an argument, is followed by its space-separated argument. For example, if and , line will be 1 abcd.
public class Edit {
private static CharStk stack = new CharStk(1000000);
private static StringStk stackStr = new StringStk(1000000);
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String st = new String();
//stackStr.push("");
for(int i=0;i<N;i++)
{
int f = sc.nextInt();
switch(f){
case 1:
String append = sc.next();
insert(append);
//stackStr.push(stack.getString());
stackStr.push(2 +" "+append.length());
//System.out.println(stack.getString());
break;
case 2:
String str = delete(sc.nextInt());
//stackStr.push(stack.getString());
stackStr.push(1 +" "+str);
//System.out.println(stack.getString());
break;
case 3:
print(sc.nextInt());
break;
case 4:
if(stackStr.getCurrentPointer()!=-1){
//stackStr.pop();
/*String ne = stackStr.peep();
stack = new CharStk(1000000);
insert(ne);*/
String ne = stackStr.pop();
String[] params = ne.split(" ");
if(params[0].equals("1")){
insert(params[1]);
}
else if(params[0].equals("2")){
delete(Integer.valueOf(params[1]));
}
}
break;
}
}
}
public static void insert(String str)
{
for(int i=0;i<str.length();i++)
{
stack.push(str.charAt(i));
}
}
public static String delete(int k)
{
String str = "";
for(int i=0;i<k;i++)
{
str = stack.pop() + str;
}
return str;
}
public static void print(int k)
{
System.out.println((char)stack.peep(k));
}
}
class CharStk {
private char[] stkArr = null;
private static char[] stkMaxArr = null;
private static int maxPointer = 0;
private int currentPointer =-1;
private int size = 0;
public CharStk(int size) {
super();
this.size = size;
stkArr = new char[size];
}
public void push(char x){
currentPointer++;
stkArr[currentPointer]=x;
}
public char pop(){
char val = stkArr[currentPointer];
stkArr[currentPointer]=0;
currentPointer--;
return val;
}
public int peep(){
if(currentPointer>-1)
return stkArr[currentPointer];
else
return -99;
}
public char peep(int i){
if(currentPointer>=i-1)
return stkArr[i-1];
else
return '!';
}
public int getCurrentPointer() {
return currentPointer;
}
public int getSize(){
return size;
}
public int getMax(){
return stkMaxArr[maxPointer];
}
public String getString()
{
String fin = "";
for(int i=0;i<currentPointer+1;i++)
{
fin = fin + stkArr[i];
}
return fin;
}
}
class StringStk {
private String[] stkArr = null;
private static String[] stkMaxArr = null;
private static int maxPointer = 0;
private int currentPointer =-1;
private int size = 0;
public StringStk(int size) {
super();
this.size = size;
stkArr = new String[size];
}
public void push(String x){
currentPointer++;
stkArr[currentPointer]=x;
}
public String pop(){
String val = stkArr[currentPointer];
stkArr[currentPointer]="";
currentPointer--;
return val;
}
public String peep(){
if(currentPointer>-1)
return stkArr[currentPointer];
else
return "";
}
public String peep(int i){
if(currentPointer>=i-1)
return stkArr[i-1];
else
return "";
}
public int getCurrentPointer() {
return currentPointer;
}
public int getSize(){
return size;
}
}