#include #include #include #include // Requires C++11. Must be compiled with -std=c++11 or higher. using LongLongString = std::vector; struct Operation { char operation; size_t index; char c; }; using Program = std::vector; Program readProgram(std::istream& in) { Program program; do { Operation op{}; in >> op.operation; if (op.operation == 'E') break; in >> op.index; if (op.operation == 'I') in >> op.c; program.emplace_back(op); } while (true); return program; } void executeProgram(const Program& p, LongLongString& s) { for (auto& op : p) { //std::cout << "Executing " << op.operation << " " << op.index << " " << op.c << " on string of size " << s.size() << std::endl; switch (op.operation) { case 'D': s.erase(s.begin() + op.index); break; case 'I': s.insert(s.begin() + op.index, int(op.c)); break; } } } bool comparePrograms(size_t N, const Program& prog_a, const Program& prog_b) { LongLongString sa(N); // use negative "characters" to mean "whatever character was initially at position i" for (size_t i = 0; i < N; ++i) sa[i] = - int(i) - 1; LongLongString sb = sa; executeProgram(prog_a, sa); executeProgram(prog_b, sb); return sa == sb; } int main(int argc, char **argv) { std::ifstream fin; if (argc >= 2) { fin.open(argv[1]); } std::istream& in = argc >= 2 ? fin : std::cin; size_t N; while (in >> N) { Program a = readProgram(in); Program b = readProgram(in); std::cout << (comparePrograms(N, a, b) ? "Identical" : "Different") << std::endl; } return 0; }