题解 AT5370 【[ABC158D] String Formation】
本题可以使用双端队列,暴力模拟肯定会TLE
有道题目和这个很像:https://www.luogu.com.cn/problem/P6823
对于维护队列,考的多的是关于翻转,翻转次数过多导致TLE
其实这道题是这样的,做标记现在是否翻转,如果要翻转,标记反过来
标记有什么用?因为如果已经翻转了,插入到前面就要变成后面,这就是标记的用处
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <deque>
using namespace std;
string s, ans = "";
deque<char> qe;
int main()
{
bool f = true;
int n;
cin >> s >> n;
int len = s.length() - 1;
for(int i = len; i >= 0; i--)
{
qe.push_front(s[i]);
}
for(int i = 1; i <= n; i++)
{
int T;
cin >> T;
if(T == 1)
{
f = !f;
}
else if(T == 2)
{
char c;
int F;
cin >> F >> c;
if(F == 1)
{
if(f)
{
qe.push_front(c);
}
else
{
qe.push_back(c);
}
}
else if(F == 2)
{
if(f)
{
qe.push_back(c);
}
else
{
qe.push_front(c);
}
}
}
}
while(!qe.empty())
{
ans.push_back(qe.front());
qe.pop_front();
}
if(!f)
{
reverse(ans.begin(), ans.end());
}
cout << ans << endl;
return 0;
}

浙公网安备 33010602011771号