洛谷云剪贴板

作者: EndSaH 发表时间: 2019-02-14 21:38 https://www.luogu.org/paste/liu5d12e 公开

$\text{P4008}$ 暴力

/**********************************************************
 * Author        : EndSaH
 * Email         : hjxhb1@gmail.com
 * Created Time  : 2019-02-11 21:56
 * FileName      : else.cpp
 * Website       : https://endsah.cf
 * *******************************************************/

#include <cstdio>
#include <cctype>
#include <cassert>
#include <vector>

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
#define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++)
#define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch))
#define __bufsize (1 << 20)

char _ibuf[__bufsize], _obuf[__bufsize], _stk[20];
char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;

struct END
{ ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } }
__;

inline int read()
{
    register int x = 0;
    register char ch;
    while (!isdigit(ch = getchar()));
    while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar()));
    return x;
}

template <typename _INT>
inline void write(_INT x)
{
    while (*++stkpos = x % 10 ^ 48, x /= 10, x);
    while (stkpos != _stk)
        putchar(*stkpos--);
}

inline void read(char* str)
{
    while (isspace(*str = getchar()));
    while (!isspace(*++str = getchar()))
        if (*str == EOF)
            break;
    *str = '\0';
}

template <typename _Tp>
inline bool Chkmax(_Tp& x, const _Tp& y)
{ return x < y ? x = y, true : false; }

template <typename _Tp>
inline bool Chkmin(_Tp& x, const _Tp& y)
{ return x > y ? x = y, true : false; }

int t, ptr, n;
char opt[6];
std::vector<char> T, temp;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("else.in", "r", stdin);
    freopen("else.out", "w", stdout);
#endif
    t = read();
    while (t--)
    {
        read(opt);
        switch (opt[0])
        {
        case 'M' :
            ptr = read();
            break;
        case 'I' :
            n = read(), temp.clear();
            for (register int i = 0; i < n; ++i)
            {
                temp.push_back('\n');
                while (temp[i] == '\n' or temp[i] == '\r')
                    temp[i] = getchar();
            }
            T.insert(T.begin() + ptr, temp.begin(), temp.end());
            break;
        case 'D' :
            T.erase(T.begin() + ptr, T.begin() + ptr + read());
            break;
        case 'G' :
            n = read();
            for (register int i = 0; i < n; ++i)
                putchar(T[i + ptr]);
            putchar('\n');
            break;
        case 'P' :
            --ptr;
            break;
        case 'N' :
            ++ptr;
            break;
        default :
            assert(false);
        }
    }
    return 0;
}

源码  复制

# $\text{P4008}$ 暴力

```cpp
/**********************************************************
 * Author        : EndSaH
 * Email         : hjxhb1@gmail.com
 * Created Time  : 2019-02-11 21:56
 * FileName      : else.cpp
 * Website       : https://endsah.cf
 * *******************************************************/

#include <cstdio>
#include <cctype>
#include <cassert>
#include <vector>

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
#define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++)
#define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch))
#define __bufsize (1 << 20)

char _ibuf[__bufsize], _obuf[__bufsize], _stk[20];
char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;

struct END
{ ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } }
__;

inline int read()
{
    register int x = 0;
    register char ch;
    while (!isdigit(ch = getchar()));
    while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar()));
    return x;
}

template <typename _INT>
inline void write(_INT x)
{
    while (*++stkpos = x % 10 ^ 48, x /= 10, x);
    while (stkpos != _stk)
        putchar(*stkpos--);
}

inline void read(char* str)
{
    while (isspace(*str = getchar()));
    while (!isspace(*++str = getchar()))
        if (*str == EOF)
            break;
    *str = '\0';
}

template <typename _Tp>
inline bool Chkmax(_Tp& x, const _Tp& y)
{ return x < y ? x = y, true : false; }

template <typename _Tp>
inline bool Chkmin(_Tp& x, const _Tp& y)
{ return x > y ? x = y, true : false; }

int t, ptr, n;
char opt[6];
std::vector<char> T, temp;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("else.in", "r", stdin);
    freopen("else.out", "w", stdout);
#endif
    t = read();
    while (t--)
    {
        read(opt);
        switch (opt[0])
        {
        case 'M' :
            ptr = read();
            break;
        case 'I' :
            n = read(), temp.clear();
            for (register int i = 0; i < n; ++i)
            {
                temp.push_back('\n');
                while (temp[i] == '\n' or temp[i] == '\r')
                    temp[i] = getchar();
            }
            T.insert(T.begin() + ptr, temp.begin(), temp.end());
            break;
        case 'D' :
            T.erase(T.begin() + ptr, T.begin() + ptr + read());
            break;
        case 'G' :
            n = read();
            for (register int i = 0; i < n; ++i)
                putchar(T[i + ptr]);
            putchar('\n');
            break;
        case 'P' :
            --ptr;
            break;
        case 'N' :
            ++ptr;
            break;
        default :
            assert(false);
        }
    }
    return 0;
}
```