about summary refs log tree commit diff
path: root/usth/MATH2.3/5/turing.cc
blob: 7ef5debddf3989c52c29dfd1aff64908256f9c1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <deque>
#include <tuple>
#include <unordered_map>

using namespace std;

int
main ()
{
  int n;
  unordered_map<int, unordered_map<int, tuple<int, int, int>>> tuples;
  cin >> n;
  while (n--)
    {
      int s, x, t, y, d;
      cin >> s >> x >> t >> y >> d;
      tuples[s][x] = {t, y, d};
    }

  deque<int> input;
  cin >> n;
  while (n--)
    {
      int i;
      cin >> i;
      input.push_back (i);
    }

  int o {0}, i {0}, s {0}, x, d;
  for (;;)
    {
      tie (s, x, d) = tuples[s][input[i - o]];
      if (!d)
        break;

      if (i < o)
        {
          input.push_front (x);
          o++;
        }
      else if (i - o == input.size ())
        input.push_back (x);
      else
        input[i - o] = x;

      if (d < 0)
        i--;
      else
        i++;
    }

  for (auto const& i : input)
    cout << i;
  cout << endl;
}