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

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

AtCoder Beginner Contest 273(E)

AtCoder Beginner Contest 273(E)

E(链式结构,思维)

E

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

下面有几种操作

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

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

\(SAVE\) \(y\),把此时的序列记录在一个题目中给出的一个\(page_y\)

\(LOAD\) \(z\):把此时的序列变成在\(page_z\)里面保存的序列里面

我之前是想过用\(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 @ 2023-06-13 21:18  righting  阅读(25)  评论(0)    收藏  举报