Най-задълбочената програма по софтуерно инженерство в България
Loading...
rudi2004 avatar rudi2004 4 Точки

Проблем със задача 08. Simple Text Editor от Java Advanced

Здравейте, имам проблем със задача 08. Simple Text Editor от Java Advanced

08.*Simple Text Editor

You are given an empty text. Your task is to implement 4 types of commands related to manipulating the text:

  • "1 [string]" - appends [string] to the end of the text
  • "2 [count]" - erases the last [count] elements from the text
  • "3 [index]" - returns the element at position [index] from the text
  • "4" - undoes the last not-undone command of type 1 or 2 and returns the text to the state before that operation

Input

  • The first line contains N, the number of operations, where 1 ≤ N ≤ 105
  • Each of the following N lines contains the name of the operation, followed by the command argument, if any, separated by space in the following format "command argument".
  • The length of the text will not exceed 1000000
  • All input characters are English letters
  • It is guaranteed that the sequence of input operation is possible to perform

Output

  • For each operation of type "3" print a single line with the returned character of that operation.

Examples

Input

Output

Comments

8

1 abc

3 3

2 3

1 xy

3 2

4

4

3 1

c

y

a

There are 8 operations. Initially, the text is empty.

Append "abc"

Print third character

Erase 3 characters

Append "xy"

Print second character

Undo last command - text is now ""

Undo last command - text is now "abc"

Print first character

 

тестовата задача  е ок, но ми дава 7 runtime error - a.

Това ми е кодът. Дали може някой да каже къде е проблемът?

https://pastebin.com/dt18K4af

 

Тагове:
0
Java Advanced 19/01/2021 07:11:48
MartinBG avatar MartinBG 3751 Точки

В стека трябва да се съхранява състоянието на текста преди да бъде изпълнена команда 1 или 2, а при команда 4 текста се заменя с това, което е най-отгоре в стека. По този начин може да имаме няколко последователни редакции, които после да бъдат премахнати една след друга.

Ето примерно решение на задачата:

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class SimpleTextEditor {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = Integer.parseInt(scanner.nextLine());

        Deque<String> stack = new ArrayDeque<>();
        String text = "";

        for (int i = 0; i < n; i++) {
            String[] token = scanner.nextLine().split(" ");

            switch (token[0]) {
            case "1":
                stack.push(text);
                text += token[1];
                break;
            case "2":
                stack.push(text);
                int count = Integer.parseInt(token[1]);
                text = text.substring(0, text.length() - count);
                break;
            case "3":
                int index = Integer.parseInt(token[1]);
                System.out.println(text.charAt(index - 1));
                break;
            case "4":
                text = stack.pop();
                break;
            }
        }
    }
}

 

0
19/01/2021 09:15:02