Cod sursa(job #3373258)

Utilizator Cosor_AlexiaCosor Alexia Cosor_Alexia Data 6 noiembrie 2026 13:44:22
Problema Componente tare conexe Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.98 kb
#include <bits/stdc++.h>
using namespace std;

set <string> sts, alphabet, fins;
map <pair<string, string>, set<string>> adj;
int t_cnt;
string init_st, line;
map <set<string>, map<string, set<string>>> dfa_adj;
vector <set<string>> dfa_sts;
set <set<string>> dfa_fins;
map <set<string>, int> dfa_id;

void read_s(set <string>& s) {
    getline(cin, line);
    stringstream ss(line);
    string x;
    while (ss >> x)
        s.insert(x);
}

set <string> lambda_closure(set <string> s) {
    set <string> res = s;
    queue <string> q;
    for (string x : s)
        q.push(x);
    while (!q.empty()) {
        string u = q.front();
        q.pop();
        for (string v : adj[{u, "lambda"}]) {
            if (!res.count(v)) {
                res.insert(v);
                q.push(v);
            }
        }
    }
    return res;
}

void build_dfa() {
    set <string> start = lambda_closure({init_st});
    dfa_sts.push_back(start);
    dfa_id[start] = 0;
    int i = 0;
    while (i < dfa_sts.size()) {
        set <string> u = dfa_sts[i];
        bool is_f = false;
        for (string x : u)
            if (fins.count(x)) is_f = true;
        if (is_f)
            dfa_fins.insert(u);
        for (string c : alphabet) {
            set <string> nxt;
            for (string x : u)
                for (string v : adj[{x, c}])
                    nxt.insert(v);
            if (!nxt.empty()) {
                nxt = lambda_closure(nxt);
                dfa_adj[u][c] = nxt;
                if (!dfa_id.count(nxt)) {
                    dfa_id[nxt] = dfa_sts.size();
                    dfa_sts.push_back(nxt);
                }
            }
        }
        i++;
    }
}

void print_dfa(bool is_min) {
    if (!is_min) {
        cout << "--- DFA ECHIVALENT ---\n";
        cout << "Numar stari: " << dfa_sts.size() << "\n";
        for (auto u : dfa_sts)
            for (string c : alphabet)
                if (dfa_adj[u].count(c))
                    cout << dfa_id[u] << " " << dfa_id[dfa_adj[u][c]] << " " << c << "\n";
    }
    else
    {
        cout << "--- DFA MINIM ---\n";
        vector <int> p(dfa_sts.size());
        for (int i = 0; i < dfa_sts.size(); i++)
            p[i] = dfa_fins.count(dfa_sts[i]) ? 1 : 0;
        bool chg = true;
        while (chg) {
            chg = false;
            map <pair<int, vector<int>>, int> grp;
            vector <int> new_p(dfa_sts.size());
            int g_id = 0;
            for (int i = 0; i < dfa_sts.size(); i++) {
                vector <int> tr;
                for (string c : alphabet) {
                    if (dfa_adj[dfa_sts[i]].count(c))
                        tr.push_back(p[dfa_id[dfa_adj[dfa_sts[i]][c]]]);
                    else
                        tr.push_back(-1);
                }
                pair <int, vector<int>> key = {p[i], tr};
                if (!grp.count(key))
                    grp[key] = g_id++;
                new_p[i] = grp[key];
            }
            if (p != new_p)
                p = new_p, chg = true;
        }
        set <int> min_sts;
        for (int x : p)
            min_sts.insert(x);
        cout << "Numar stari: " << min_sts.size() << "\n";
        set <string> seen;
        for (int i = 0; i < dfa_sts.size(); i++) {
            for (string c : alphabet) {
                if (dfa_adj[dfa_sts[i]].count(c)) {
                    int from = p[i];
                    int to = p[dfa_id[dfa_adj[dfa_sts[i]][c]]];
                    string row = to_string(from) + " " + to_string(to) + " " + c;
                    if (!seen.count(row)) {
                        cout << row << "\n";
                        seen.insert(row);
                    }
                }
            }
        }
    }
    cout << "\n";
}

int main()
{
    read_s(sts);
    read_s(alphabet);
    cin >> t_cnt;
    for (int i = 0; i < t_cnt; i++) {
        string u, v, c;
        cin >> u >> v >> c;
        adj[{u, c}].insert(v);
    }
    cin >> init_st >> ws;
    read_s(fins);
    build_dfa();
    print_dfa(false);
    print_dfa(true);

    return 0;
}