about summary refs log tree commit diff
path: root/usth/MATH2.3/5/nfa.cc
blob: 840120212928c2cf9f6193901f69ba368ed507b4 (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
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

bool
recognize (unordered_map<int, unordered_map<int, vector<int>>>& f,
           unordered_map<int, bool>& F, string& s, int i, int r)
{
  if (i >= s.size ())
    return F[r];
  for (auto const& t : f[r][s[i] - '0'])
    if (recognize (f, F, s, i + 1, t))
      return true;
  return false;
}

int
main ()
{
  int n;
  unordered_map<int, unordered_map<int, vector<int>>> f;
  cin >> n;
  while (n--)
    {
      int s, i, r;
      cin >> s >> i >> r;
      f[s][i].push_back (r);
    }

  unordered_map<int, bool> F;
  cin >> n;
  while (n--)
    {
      int s;
      cin >> s;
      F[s] = true;
    }

  string s;
  cin >> s;
  cout << (recognize (f, F, s, 0, 0) ? "yes" : "no") << endl;
}