ZeroJudge A870: List Maker

Maintaining a frequently changing list of items or storing input/output in a storage device is a common task in programming languages. The recorded items could be university applicants awaiting approval. The list should provide different operations according to various instructions, such as insertion or deletion.

Write a program based on the following functionality definition, which should provide the following commands: add, insert, and remove.

ADD X: Add X to the bottom of the list. This command must be executable on an empty list.

INSERT X N: Insert X before N.

REMOVE X: Remove X from the list.

X and N contain only uppercase English characters.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs: the input items will not repeat. The command "SHOW" indicates the end of input.The program must output the list in the correct order, with list items separated by a single space.
ADD NEVER
ADD COLLAR
INSERT CAT COLLAR
ADD DOG
ADD SCARES
INSERT ANYTHING CAT
REMOVE CAT
INSERT THAT SCARES
REMOVE COLLAR
INSERT WEAR ANYTHING
REMOVE DOG
ADD CAT
INSERT YOUR CAT
SHOW
NEVER WEAR ANYTHING THAT SCARES YOUR CAT

Thought Process

You can use the push_back function of the vector to handle the ADD action. For INSERT, you can iterate through the original array using a for loop, copy its elements to a new array, and determine whether the current data is the one to be inserted. If it is, you can first push_back the specified data and then push_back the original data at that position. The logic for REMOVE is similar to INSERT. After INSERT and REMOVE, you can use the assign function to copy the new array back to the old array.

Sample Code-ZeroJudge A870: List Maker

#include <iostream>
#include <vector>
using namespace std;

int main() {
    string command;
    vector<string>v;
    while (cin >> command && command != "SHOW")
    {
        if (command == "ADD")
        {
            string str;
            cin >> str;
            v.push_back(str);
        }
        else if (command == "INSERT")
        {
            string X, N;
            cin >> X >> N;
            vector<string>tmp;
            for (int i = 0; i<v.size(); i++)
            {
                if (v[i] == N) tmp.push_back(X);
                tmp.push_back(v[i]);
            }
            v.clear();
            v.assign(tmp.begin(), tmp.end());
        }
        else
        {
            string str;
            cin >> str;
            vector<string>tmp;
            for(int i = 0; i<v.size(); i++)
            {
                if (v[i] != str) tmp.push_back(v[i]);
            }
            v.clear();
            v.assign(tmp.begin(), tmp.end());
        }
    }
    for (int i = 0; i<v.size(); i++)
    {
        cout << v[i] << " ";
    }
    cout << "\n";
}

//ZeroJudge A870
//Dr. SeanXD

Comments