题解 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;
}
posted @   HappyBobb  阅读(5)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示