直接模拟操作的话肯定会超时,因为每次添加的是一个字符,我们就可以根据翻转次数判断是加在前面还是后面。但是直接用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;
}
来源:CSDN
作者:邵光亮
链接:https://blog.csdn.net/qq_43627087/article/details/104749172