AtCoder Beginner Contest 158 D - String Formation

℡╲_俬逩灬. 提交于 2020-03-09 12:49:12

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
直接模拟操作的话肯定会超时,因为每次添加的是一个字符,我们就可以根据翻转次数判断是加在前面还是后面。但是直接用string±还是会超时,换了 insert才过。

AC代码:

int m, p;
int ans, res, x;
int n, q, k;
string s, t;
int op;
int cnt;
char c;
int main()
{
    cin >> s;
    sd(q);
    cnt = 0;
    while (q--)
    {
        sd(op);
        if (op == 1)
        {
            cnt++;
        }
        else
        {
            sd(k);
            cin>>c;
            if (cnt & 1)
            {
                if (k == 1)
                    s.insert(s.end(), c);
                else
                    s.insert(s.begin(), c);
            }
            else
            {
                if (k == 1)
                    s.insert(s.begin(), c);
                else
                    s.insert(s.end(), c);
            }
        }
    }
    if (cnt & 1)
        reverse(s.begin(), s.end());
    cout << s;
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!