Dang_Quan_10_Tin

SCRIVENER

Sep 8th, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #define task "SCRIVENER"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 1e6 + 5;
  12. struct node
  13. {
  14.     int dep;
  15.     char v;
  16.     node *child[26], *par[20];
  17.     node()
  18.     {
  19.         dep = 1;
  20.         v = 'a';
  21.         for (int i = 0; i < 26; ++i)
  22.         {
  23.             child[i] = NULL;
  24.             if (i < 20)
  25.                 par[i] = NULL;
  26.         }
  27.     }
  28. } root;
  29. node *backup[N], *proot;
  30.  
  31. int n, m;
  32.  
  33. void Read()
  34. {
  35.     cin >> n;
  36. }
  37.  
  38. void Add(char c)
  39. {
  40.     if (!(proot->child[c - 'a']))
  41.         proot->child[c - 'a'] = new node;
  42.  
  43.     proot->child[c - 'a']->par[0] = proot;
  44.     proot->child[c - 'a']->dep = proot->dep + 1;
  45.     proot = proot->child[c - 'a'];
  46.     proot->v = c;
  47.  
  48.     for (int i = 1; i < 20; ++i)
  49.         proot->par[i] = (!(proot->par[i - 1])) ? NULL : proot->par[i - 1]->par[i - 1];
  50.  
  51.     backup[++m] = proot;
  52. }
  53.  
  54. char Get(int x)
  55. {
  56.     node *a = proot;
  57.     for (int i = 19; ~i; --i)
  58.         if (a->par[i] && a->par[i]->dep >= x)
  59.             a = a->par[i];
  60.     return a->v;
  61. }
  62.  
  63. void Solve()
  64. {
  65.     m = 1;
  66.     backup[1] = &root;
  67.     proot = &root;
  68.  
  69.     while (n--)
  70.     {
  71.         char t;
  72.         cin >> t;
  73.         if (t == 'T')
  74.         {
  75.             char c;
  76.             cin >> c;
  77.             Add(c);
  78.         }
  79.         else if (t == 'U')
  80.         {
  81.             int k;
  82.             cin >> k;
  83.             proot = backup[m - k];
  84.             backup[++m] = proot;
  85.         }
  86.         else
  87.         {
  88.             int x;
  89.             cin >> x;
  90.             cout << Get(x + 2) << "\n";
  91.         }
  92.     }
  93. }
  94.  
  95. int32_t main()
  96. {
  97.     ios::sync_with_stdio(0);
  98.     cin.tie(0);
  99.     cout.tie(0);
  100.     if (fopen(task ".INP", "r"))
  101.     {
  102.         freopen(task ".INP", "r", stdin);
  103.         freopen(task ".OUT", "w", stdout);
  104.     }
  105.     Read();
  106.     Solve();
  107. }
  108.  
Add Comment
Please, Sign In to add comment