// August Druzgal
// 9/16/2023
// Code for CIS 3362 Fall 23 Hmk 2B Question 1

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

class Hmk2B1
{
    public static void main(String[] args) 
    {
        String cyphertext;
        String subs[] = new String[512];
        String output = "";
        
        // Read all of the decoding information from the input file
        try
        {
            File input = new File("input2B1.txt");
            BufferedReader reader = new BufferedReader(new FileReader(input));

            // The cyphertext is the first string at the top of the file
            cyphertext = reader.readLine();
            String str = reader.readLine();
            
            // subs maps every encoded character to the string it decodes to
            while (str != null)
            {
                subs[str.charAt(0)] = str.substring(2);
                str = reader.readLine();
            }

            reader.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return;
        }

        char c;

        // loop through the cyphertext, decoding each character
        for (int i = 0; i < cyphertext.length(); i++)
        {
            c = cyphertext.charAt(i);

            // If a character maps to "Null", ignore it. 
            if (subs[c].equals("Null"))
            {
                continue;
            }

            // If a character maps to "Backspace", remove the previously inserted character/string 
            if (subs[c].equals("Backspace"))
            {
                // When you backspace a null character, you don't need to remove anything
                if (subs[cyphertext.charAt(i - 1)].equals("Null"))
                    continue;
                
                // Otherwise, remove everything you appended in the last step
                output = output.substring(0, output.length() - subs[cyphertext.charAt(i - 1)].length());
                continue;
            }

            output = output.concat(subs[c]);
        }

        System.out.println(output);
    }
}
