about summary refs log tree commit diff
path: root/usth/MATH2.3/5/grammar-to-automata.cc
blob: 125b3c974965460207ed9c227aad9647553d1daf (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
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int
main ()
{
  int n;
  cin >> n;

  unordered_map<char, unordered_map<int, char>> automata;
  unordered_map<char, int> finals;
  for (int i = 0; i < n; ++i)
    {
      char c;
      string s, t;
      cin >> c >> t >> s;
      if (s == "$")
        finals[c] = 1;
      else
        automata[c][s[0] - '0'] =  (s.length () == 1) ? '$' : s[1];
    }

  cout << automata.size () << endl;
  for (auto& [c, m] : automata)
    cout << c << '\t' << m[0] << '\t' << m[1] << '\t'  << finals[c] << endl;
}