Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- map<string, map<char, set<string>>> go;
- map<string, set<string>> eps;
- set<string> fin, used;
- void dfs(const string& from) {
- used.insert(from);
- for (const auto& to : eps[from]) {
- if (used.find(to) == used.end()) {
- dfs(to);
- }
- }
- }
- int main() {
- string from, c, to;
- cin >> from;
- while (from != "END") {
- cin >> c >> to;
- if (c != "eps") {
- go[from][c[0]].insert(to);
- } else {
- eps[from].insert(to);
- }
- cin >> from;
- }
- cin >> to;
- while (to != "END") {
- fin.insert(to);
- cin >> to;
- }
- string start, check;
- cin >> start >> check;
- set<string> can = {start};
- for (size_t i = 0; i < check.length(); ++i) {
- used.clear();
- for (const auto& s : can) {
- dfs(s);
- }
- can = used;
- char ch = check[i];
- set<string> nxt;
- for (const auto& s : can) {
- for (const auto& item : go[s][ch]) {
- nxt.insert(item);
- }
- }
- can = nxt;
- if (can.empty()) {
- cout << "0\n" << i << "\n";
- return 0;
- }
- }
- bool ok = false;
- for (const auto& item : fin) {
- if (can.find(item) != can.end()) {
- ok = true;
- break;
- }
- }
- if (ok) {
- cout << "1";
- } else {
- cout << "0";
- }
- cout << "\n" << check.size() << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment