about summary refs log tree commit diff
path: root/usth/MATH2.3/5/grammar-to-automata.cc
diff options
context:
space:
mode:
Diffstat (limited to 'usth/MATH2.3/5/grammar-to-automata.cc')
-rw-r--r--usth/MATH2.3/5/grammar-to-automata.cc29
1 files changed, 29 insertions, 0 deletions
diff --git a/usth/MATH2.3/5/grammar-to-automata.cc b/usth/MATH2.3/5/grammar-to-automata.cc
new file mode 100644
index 0000000..125b3c9
--- /dev/null
+++ b/usth/MATH2.3/5/grammar-to-automata.cc
@@ -0,0 +1,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;
+}