Problem:
Problem C: Emacs-like Editor

Emacs is a text editor which is widely used by many programmers.

The advantage of Emacs is that we can move a cursor without arrow keys and the mice. For example, the cursor can be moved right, left, down, and up by pushing f, b, n, p with the Control Key respectively. In addition, cut-and-paste can be performed without the mouse.

Your task is to write a program which simulates key operations in the Emacs-like editor. The program should read a text and print the corresponding edited text.

The text consists of several lines and each line consists of zero or more alphabets and space characters. A line, which does not have any character, is a blank line.

The editor has a cursor which can point out a character or the end-of-line in the corresponding line. The cursor can also point out the end-of-line in a blank line.

In addition, the editor has a buffer which can hold either a string (a sequence of characters) or a linefeed.

The editor accepts the following set of commands (If the corresponding line is a blank line, the word "the first character" should be "the end-of-line"):

a

Move the cursor to the first character of the current line.

e

Move the cursor to the end-of-line of the current line.

p

Move the cursor to the first character of the next upper line, if it exists.

If there is no line above the current line, move the cursor to the first character of the current line.

n

Move the cursor to the first character of the next lower line, if it exists.

If there is no line below the current line, move the cursor to the first character of the current line.

f

Move the cursor by one character to the right, unless the cursor points out the end-of-line.

If the cursor points out the end-of-line and there is a line below the current line, move the cursor to the first character of the next lower line. Otherwise, do nothing.

b

Move the cursor by one character to the left, unless the cursor points out the first character.

If the cursor points out the first character and there is a line above the current line, move the cursor to the end-of-line of the next upper line. Otherwise, do nothing.

d

If the cursor points out a character, delete the character (Characters and end-of-line next to the deleted character are shifted to the left).

If the cursor points out the end-of-line and there is a line below, the next lower line is appended to the end-of-line of the current line (Lines below the current line are shifted to the upper).
Otherwise, do nothing.

k

If the cursor points out the end-of-line and there is a line below the current line, perform the command d mentioned above, and record a linefeed on the buffer.

If the cursor does not point out the end-of-line, cut characters between the cursor (inclusive) and the end-of-line, and record them on the buffer. After this operation, the cursor indicates the end-of-line of the current line.

y

If the buffer is empty, do nothing.

If the buffer is holding a linefeed, insert the linefeed at the cursor. The cursor moves to the first character of the new line.

If the buffer is holding characters, insert the characters at the cursor. The cursor moves to the character or end-of-line which is originally pointed by the cursor.

The cursor position just after reading the text is the beginning of the first line, and the initial buffer is empty.

Input

The input consists of only one data-set which includes two parts. The first part gives a text consisting of several lines. The end of the text is indicated by a line (without quotes):

"END_OF_TEXT"

This line should not be included in the text.

Next part gives a series of commands. Each command is given in a line. The end of the commands is indicated by a character '-'.

Output

For the input text, print the text edited by the commands.

Constraints

The number of lines in the text given as input ≤ 10

The number of characters in a line given as input ≤ 20

The number of commands ≤ 300

The maximum possible number of lines in the text during operations ≤ 100

The maximum possible number of characters in a line during operations ≤ 1000

Sample Input

hyo
ni
END_OF_TEXT
f
d
f
f
k
p
p
e
y
a
k
y
y
n
y
-

Output for the Sample Input

honihoni
honi

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static final int MAXINPUTLINE = 10;
    static final int MAXINPUTCOMMAND = 300;
    static final int MAXMODIFIEDTEXTLINE = 100;
    static final int MAXMODIFIERTEXTCHAR = 1000;
    static final String ENDOFLINE = "$";
    static Scanner scan = new Scanner(System.in);
    static ArrayList<String> modifyingText = new ArrayList<String>();
    static String buffer;
    static int cursorLocLine = 0;
    static int cursorLocRow = 0;

    public static void main(String args[]) {
        ArrayList<String> inputText;
        ArrayList<String> inputCommand;

        inputText = giveTextLine();
//        System.out.println("==============================");
//        System.out.println("give Text Success");

        inputCommand = giveCommand();

//        System.out.println("==============================");
//        System.out.println("give Command Success");
        modifyText(inputText, inputCommand);

        for (int i = 0; i < modifyingText.size(); i++) {
            int outLen = modifyingText.get(i).length();
            if (outLen <= 1) {
                System.out.print("\n");
            } else System.out.println(modifyingText.get(i).substring(0, outLen - 1));
        }
    }

    private static ArrayList<String> giveTextLine() {
        String input;
        ArrayList<String> inputText = new ArrayList<>();
//        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < MAXINPUTLINE; i++) {
            input = scan.nextLine();
            if (input.equals("END_OF_TEXT")) break;
            inputText.add(i, input);
        }
        return inputText;
    }

    private static ArrayList<String> giveCommand() {
        String input;
        ArrayList<String> inputCommand = new ArrayList<>();
//        Scanner scan = new Scanner(System.in);
        for (int i = 0; i < MAXINPUTCOMMAND; i++) {
            input = scan.nextLine();
            if (input.equals("-")) break;
            inputCommand.add(i, input);
        }
        return inputCommand;
    }

    private static void modifyText(final ArrayList<String> inputText, final ArrayList<String> inputCommand) {

        for (int i = 0; i < inputText.size(); i++) {
            modifyingText.add(i, inputText.get(i) + ENDOFLINE);
        }

        for (int i = 0; i < inputCommand.size(); i++) {
//            System.out.println("===============================");
//            System.out.println(inputCommand.get(i) + " " + cursorLocRow + " " + cursorLocLine + " " + modifyingText.get(cursorLocLine).length() + " " + buffer);
//            System.out.println("===============================");
//            modifyingText.forEach(System.out::println);

            if (inputCommand.get(i).equals("a")) editA();
            else if (inputCommand.get(i).equals("e")) editE();
            else if (inputCommand.get(i).equals("p")) editP();
            else if (inputCommand.get(i).equals("n")) editN();
            else if (inputCommand.get(i).equals("f")) editF();
            else if (inputCommand.get(i).equals("b")) editB();
            else if (inputCommand.get(i).equals("d")) editD();
            else if (inputCommand.get(i).equals("k")) editK();
            else if (inputCommand.get(i).equals("y")) editY();
        }
    }

    private static String remove(String str, int removeIndex) {
        StringBuilder builder = new StringBuilder(str);
        return builder.deleteCharAt(removeIndex).toString();
    }

    private static String insert(String str, int insertIndex) {
        StringBuilder builder = new StringBuilder(str);

        if (builder.length() <= insertIndex) {
            return builder.append(buffer).toString();
        } else {
            return builder.insert(insertIndex, buffer).toString();
        }
    }

    private static void editA() {
        cursorLocRow = 0;
    }

    private static void editE() {
        cursorLocRow = currentLineString().length() - 1;
    }

    private static void editP() {
        cursorLocRow = 0;
        if (cursorLocLine > 0) cursorLocLine--;
    }

    private static void editN() {
        cursorLocRow = 0;
        if (isLastLine()) return;
        cursorLocLine++;
    }

    private static void editF() {
        if (isLastRow()) {
            if (isLastLine()) return;
            cursorLocLine++;
            cursorLocRow = 0;
        } else {
            cursorLocRow++;
        }
    }

    private static void editB() {
        if (cursorLocRow > 0) cursorLocRow--;
        else if (cursorLocLine > 0) {
            cursorLocRow = modifyingText.get(cursorLocLine - 1).length() - 1;
            cursorLocLine--;
        }
    }

    private static void editD() {
        if (!isLastRow()) {
            replaceCurrentLine(remove(modifyingText.get(cursorLocLine), cursorLocRow));
        } else {
            if (currentLineString().length() == 1) {
                modifyingText.remove(cursorLocLine);
            } else if (!isLastLine()) {
                replaceCurrentLine(currentLineString() + modifyingText.get(cursorLocLine + 1));
                replaceCurrentLine(remove(currentLineString(), cursorLocRow));
                for (int i = cursorLocLine + 1; i < modifyingText.size(); i++) {
                    if (isLastLine(i + 1)) break;
                    replaceLine(i, modifyingText.get(i + 1));
                }
            }
        }
    }

    private static void editK() {
        if (isLastRow()) {
            if (!isLastLine()) {

                replaceCurrentLine(currentLineString() + modifyingText.get(cursorLocLine + 1));
                replaceCurrentLine(remove(currentLineString(), cursorLocRow));

                for (int i = cursorLocLine + 1; i < modifyingText.size(); i++) {
                    if (isLastLine(i + 1))
                        replaceLine(i, modifyingText.get(i + 1));
                }
                modifyingText.remove(lastLine());
                buffer = "\n";
            }
        } else {
            buffer = currentLineString().substring(cursorLocRow, currentLineString().length() - 1);
            if (cursorLocRow == 0) replaceCurrentLine(ENDOFLINE);
            else replaceCurrentLine(currentLineString().substring(0, cursorLocRow) + ENDOFLINE);
            cursorLocRow = currentLineString().length() - 1;
        }
    }

    private static void editY() {
        if (buffer != null) {
            if (buffer.equals("\n")) {

                if (isLastRow()) {
                    String movingText = currentLineString().substring(cursorLocRow);
                    replaceCurrentLine(currentLineString().substring(0, cursorLocRow) + ENDOFLINE);
                    insertLine(cursorLocLine + 1, movingText);
                } else {
                    String movingText = currentLineString().substring(cursorLocRow);
                    insertLine(cursorLocLine + 1, movingText);

                    if (cursorLocRow != 0) {
                        replaceCurrentLine(currentLineString().substring(0, cursorLocRow) + ENDOFLINE);
                    } else {
                        replaceCurrentLine(ENDOFLINE);
                    }
                }
                cursorLocRow = 0;
                cursorLocLine++;

            } else {
                replaceCurrentLine(insert(currentLineString(), cursorLocRow));
                cursorLocRow += buffer.length();
            }
        }
    }

    private static String currentLineString() {
        return modifyingText.get(cursorLocLine);
    }

    private static void replaceLine(int index, String str) {
        modifyingText.set(index, str);
    }

    private static void replaceCurrentLine(String str) {
        modifyingText.set(cursorLocLine, str);
    }

    private static void insertLine(int index, String str) {
        modifyingText.add(index, str);
    }

    private static void insertInCurrentLine(String str) {
        modifyingText.add(cursorLocLine, str);
    }

    private static boolean isLastLine() {
        return modifyingText.size() <= cursorLocLine + 1;
    }

    private static boolean isLastLine(int i) {
        return modifyingText.size() > i;
    }

    private static boolean isLastRow() {
        return modifyingText.get(cursorLocLine).length() <= cursorLocRow + 1;
    }

    private static int lastLine() {
        return modifyingText.size() - 1;
    }

    private static boolean isEOL() {
        return String.valueOf(currentLineString().charAt(cursorLocRow)).equals(ENDOFLINE);
    }
}