每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

AtCoder Beginner Contest 273(E)

AtCoder Beginner Contest 273(E)

E(链式结构,思维)

E

题目大意就是原本有一个空的序列,我们后面会进行q次操作,每次操作我们都需要输出此时的序列的最后数字

下面有几种操作

ADD x,代表在这在这个序列的最后面添加一个x

DELETE,代表如果此时的序列存在数字的话,那么我们就移除最后一个数字

SAVE y,把此时的序列记录在一个题目中给出的一个pagey

LOAD z:把此时的序列变成在pagez里面保存的序列里面

我之前是想过用map<int,vector<int>>,但是它re

ac的解法用的是链式结构

我们先用一个now随着每次操作的标记

添加一个数字,我们会在数组里面添加上x,而且我们还会保存此时的now标记,这样我们后面在实现保存page和获取page里面的数组,我们每次保存就是保存now,后面假如我们需要用到用到page,我们就会调用里面的标记now,然后把这个值赋给此时的now,同样删除一个数字就是把当前的now赋给它的上一个now

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include<cmath>
#include <unordered_map>
#include <array>
#include <cstring>
#include <bitset>
#include <numeric>
using namespace std;
#define int long long 
#define LL long long
#define ios  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define inf 1e18
#define INF 1e18
#define eps 1e-6
#define mem(a,b) memset((a),(b),sizeof(a))
const int maxn=5e5+10;
const int mod=998244353;
int q;
map<int,int>bk;
vector<pair<int,int>>res;
int now;
signed main ()
{
  ios;
  cin>>q;
  res.push_back({0,-1});
  now=0;
  while (q--)
  {
    string op;
    cin>>op;
    if(op=="ADD")
    {
      int x;
      cin>>x;
      res.push_back({now,x});
      now=res.size()-1;
    }
    else if(op=="DELETE")
    {
      now=res[now].first;
    }
    else if(op=="SAVE")
    {
      int x;
      cin>>x;
      bk[x]=now;
    }
    else if(op=="LOAD")
    {
      int x;
      cin>>x;
      now=bk[x];
    }
    cout<<res[now].second<<" ";
  }
  system ("pause");
  return 0;
}
posted @   righting  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示