Cod sursa(job #2583234)

Utilizator fminostress9FMI No Stress 9 fminostress9 Data 17 martie 2020 22:19:54
Problema NFA Scor Ascuns
Compilator cpp-64 Status done
Runda Marime 0.83 kb
#include <bits/stdc++.h>

using namespace std;
using Bitset = bitset<512>;

int main() {
  ifstream cin("nfa.in");
  ofstream cout("nfa.out");

  int n, m, k; cin >> n >> m >> k;
  vector<vector<Bitset>> graph(n, vector<Bitset>(26));

  Bitset fini;
  int x; cin >> x; --x;
  while (k--) {
    int x; cin >> x; --x;
    fini[x] = true;
  }

  for (int i = 0; i < m; ++i) {
    int a, b; char c; cin >> a >> b >> c;
    --a; --b; c -= 'a';
    graph[a][c][b] = true;
  }
  int q; cin >> q;
  while (q--) {
    string s; cin >> s;
    Bitset now; now[x] = true;
    for (auto c : s) {
      Bitset nxt;
      for (int i = 0; i < n; ++i) 
        if (now[i]) nxt |= graph[i][c - 'a'];
      swap(now, nxt);
    }
    now &= fini;
    bool ok = false;
    for (int i = 0; i < n; ++i) 
      if (now[i]) ok = true;
    cout << ok << '\n';
  }
  return 0;
}