#include <stdio.h>
#include <string.h>
#include <vector>


using namespace std;

static const int DATA_MAX = 1001;
static const int DATA_MIN = 0;

typedef struct _READORDER
{
    char order[8];
    int start;
    int end;
    char replace[1000];
} READORDER;

class OrderBase
{
private:
    enum { MAX = 1001 };
    static char str[MAX];

public:
    OrderBase(char s[]);
    virtual ~OrderBase() {}

    virtual bool execute(int start, int end, char target[]) = 0;

    char* getStr() const { return str; }
};

class PrintOrder final : public OrderBase
{
public:
    PrintOrder(char s[]) : OrderBase(s) {}
    virtual ~PrintOrder() {}

public:
    virtual bool execute(int start, int end, char target[]);
};

class ReplaceOrder final : public OrderBase
{
private:
    char replace[DATA_MAX];

public:
    ReplaceOrder(char s[]) : OrderBase(s) { memcpy(replace, s, DATA_MAX); }
    virtual ~ReplaceOrder() {}

    virtual bool execute(int start, int end, char target[]);

};

class ReverseOrder final : public OrderBase
{
public:
    ReverseOrder(char s[]) : OrderBase(s) {}
    virtual ~ReverseOrder() {}

public:
    virtual bool execute(int start, int end, char target[]);
};

char OrderBase::str[DATA_MAX];

OrderBase::OrderBase(char s[])
{
    memset(str, 0, MAX);

    if (0 < strlen(s) && MAX > strlen(s))
        memcpy(str, s, strlen(s));
}

bool PrintOrder::execute(int start, int end, char target[])
{
    for (int i = start; i <= end; i++) {
        printf("%c", getStr()[i]);
    }
    putchar('\n');

    return true;
}

bool ReplaceOrder::execute(int start, int end, char target[])
{
    if ((end - start + 1) != strlen(target))
        return false;

    int cnt = 0;
    for (int i = start; i <= end; i++) {
        getStr()[i] = target[cnt++];
    }
    return true;
}

bool ReverseOrder::execute(int start, int end, char target[])
{
    char c;

    if (start >= end)
        return true;

    c = *(getStr() + start);
    *(getStr() + start) = *(getStr() + end);
    *(getStr() + end) = c;

    execute(++start, --end, "");

    return false;
}

bool read(char orig[], vector<READORDER>& orders)
{
    int read_max = 0;

    scanf("%s", orig);
    scanf("%d", &read_max);

    for (int i = 0; i < read_max; i++) {
        READORDER data = { "", 0, 0, "" };
        scanf("%s", &data.order);   if (DATA_MAX < strlen(data.order)) { return false; }
        scanf("%d", &data.start);   if (DATA_MIN > data.start) { return false; }
        scanf("%d", &data.end);     if (DATA_MAX < data.end) { return false; }
        if (0 != strcmp("replace", data.order)) {
            orders.push_back(data);
            continue;
        }

        scanf("%s", &data.replace); if (DATA_MAX < strlen(data.replace)) { return false; }
        orders.push_back(data);
    }

    return true;
}

bool write(char orig[], vector<READORDER>& orders)
{
    OrderBase* order = new PrintOrder(orig);

    char _tmp[DATA_MAX];

    for (vector<READORDER>::iterator it = orders.begin(); orders.end() != it; it++) {
        memset(_tmp, 0, DATA_MAX);
        memcpy(_tmp, order->getStr(), strlen(order->getStr()));
        delete order;
        order = NULL;
        
        if (0 == strcmp("print", it->order))
            order = new PrintOrder(_tmp);
        if (0 == strcmp("replace", it->order))
            order = new ReplaceOrder(_tmp);
        if (0 == strcmp("reverse", it->order))
            order = new ReverseOrder(_tmp);

        order->execute(it->start, it->end, it->replace);
    }
    delete order;

    return true;
}

int main()
{
    char orig[DATA_MAX];
    vector<READORDER> vec;

    memset(orig, 0, DATA_MAX);

    (void)read(orig, vec);

    (void)write(orig, vec);

}



Measured memory_bytes: 2824