We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
For Java 7.
Ok. You need to know that i generated this by chatGPT, tell him to optimize my oringinal. Eventhrough it's still not good enough, but try a few times then it might pass.
import java.util.Scanner;
import java.util.Stack;
public class SolutionSolution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int q = scanner.nextInt();
scanner.nextLine();
StringBuilder currentText = new StringBuilder();
Stack<Operation> history = new Stack<>();
for (int i = 0; i < q; i++) {
int type = scanner.nextInt();
switch (type) {
case 1: // Append
String w = scanner.next();
history.push(new Operation(1, w.length()));
currentText.append(w);
break;
case 2: // Delete
int k = scanner.nextInt();
String deletedText = currentText.substring(currentText.length() - k);
history.push(new Operation(2, deletedText));
currentText.delete(currentText.length() - k, currentText.length());
break;
case 3: // Print
int pos = scanner.nextInt();
System.out.println(currentText.charAt(pos - 1));
break;
case 4: // Undo
if (!history.isEmpty()) {
Operation lastOperation = history.pop();
if (lastOperation.type == 1) {
// Undo append: remove the last 'length' characters
currentText.delete(currentText.length() - lastOperation.length, currentText.length());
} else if (lastOperation.type == 2) {
// Undo delete: append the deleted string
currentText.append(lastOperation.data);
}
}
break;
default:
break;
}
}
scanner.close();
}
static class Operation {
int type; // 1 for append, 2 for delete
int length;
String data;
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
For Java 7. Ok. You need to know that i generated this by chatGPT, tell him to optimize my oringinal. Eventhrough it's still not good enough, but try a few times then it might pass.
import java.util.Scanner; import java.util.Stack;
public class SolutionSolution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); scanner.nextLine();
static class Operation { int type; // 1 for append, 2 for delete int length; String data;
}