aboutsummaryrefslogtreecommitdiff
path: root/semestr-6/jfizo/rozwy/gen.cpp
blob: a6a298873af95b78b4fa76a0809697ce9fb9bfd0 (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <vector>
#include <assert.h>
#include <unordered_map>
#include <string>
#include <algorithm>

using namespace std;

typedef vector<int> transitions;
typedef vector<int> word;

struct Automaton {
    int states;
    vector<int> accept;
    vector<transitions> delta;
    int initial;

    Automaton(int in, int n, const vector<transitions> &d, const vector<int> &acc) {
        assert(in < n);
        assert(d.size() == n);
        
        accept = acc;
        delta.resize(n);
        for (int i = 0; i < d.size(); i++) {
            delta[i] = transitions(d[i]);
        }
        in = initial;
        states = n;
    }

    int apply(const word &w, int beg) {
        int crawl = beg;
        for (auto c: w) {
            crawl = delta[crawl][c];
        }
        return crawl;
    }

    bool check(const word &s) {
        int q = apply(s, initial);
        for (auto i: accept) {
            if (q == i)
                return true;
        }
        return false;
    }

    void dump() {
        cout << "States: " << states << "; accepting: ";
        for (auto i: accept) cout << i << " ";
        cout << "\n";
        cout << "Transition table:\n";
        for (int i = 0; i < states; i++) {
            cout << i << ":";
            for (int j = 0; j < delta[i].size(); j++) {
                cout << "\t--" << (char)(j + 'a') << "--> " << delta[i][j] << "\n";
            }
        }
    }
};

void fill_accepts(int mask, int states, vector<int> &acc) {
    acc.clear();
    for (int i = 0; i < states; i++) {
        if (mask & (1<<i))
            acc.push_back(1);
        else  
            acc.push_back(0);
    }
}

bool nextperm(int states, transitions &t, int alph_sz) {
    for (int s = 0; s < alph_sz; s++) {
        t[s]++;
        if (t[s] == states) {
            t[s] = 0;
        }
        else {
            return true;
        }
    }
    return false;
}

void reset_transition(transitions &t, int alph_sz) {
    for (int s = 0; s < alph_sz; s++) {
        t[s] = 0;
    }
}

bool next_word(word &w, int alph_sz) {
    for (int i = 0; i < w.size(); i++) {
        w[i]++;
        if (w[i] == alph_sz) {
            w[i] = 0;
        } else return true;
    }
    return false;
}

vector<word> generate_words(int max_len, int alph_sz) {
    vector<word> result;
    for (int len = 0; len <= max_len; len++) {
        word w;
        for (int i = 0; i < len; i++) w.push_back(0);
        do {
            result.push_back(w);
        } while (next_word(w, alph_sz));
    }
    return result;
}

void get_transitions(Automaton &aut, word &w, vector<int> &res) {
    res.clear();
    for (int i = 0; i < aut.states; i++) {
        res.push_back(aut.apply(w, i));
    }
}

int my_power(int a, int b) {
    if (b == 0) return 1;
    return a * my_power(a, b-1);
}

bool check_automaton(vector<word> &words, Automaton &aut) {
    vector<vector<int>> funcs;
    for (auto w: words) {
        vector<int> trans;
        get_transitions(aut, w, trans);
        funcs.push_back(trans);            
    }
    sort(funcs.begin(), funcs.end());
    auto it = unique(funcs.begin(), funcs.end());
    funcs.erase(it, funcs.end());

    cout << funcs.size() << "\n";
    if (funcs.size() == my_power(aut.states, aut.states)) {
        cout << "mamy to!!\n";
        aut.dump();
        return true;
    }
    return false;
}

vector<Automaton> generate_automatons(int states, int alph_sz, vector<word> &words) {   
    vector<Automaton> result;
    vector<transitions> delta;
    vector<int> accepts;

    for (int i = 0; i < states; i++) {
        delta.push_back(transitions());
        for (int s = 0; s < alph_sz; s++) {
            delta[i].push_back(0);
        }
    }

    /* loop over accepting states */
    // for (int i = 0; i < (1<<states); i++) {
    //     if (i == 0) continue;
    fill_accepts(1, states, accepts);
    
    /* loop over possible transitions */
    bool last = false;
    while (!last) {
        Automaton a(0, states, delta, accepts);
        if (check_automaton(words, a)) {
            return result;
        }
        // result.push_back(a);
        last = true;
        for (int i = 0; i < states; i++) {
            if (nextperm(states, delta[i], alph_sz)) {
                last = false;
                break;
            }
            else {
                reset_transition(delta[i], alph_sz);
            }
        }   
    }
    // }

    return result;
}

Automaton automaton_from_input() {
    int states, alph_sz;
    cin >> states >> alph_sz;
    
    vector<int> accept;
    vector<transitions> delta;
    int initial;

    fill_accepts(1, states, accept);

    initial = 0;
    for (int i = 0; i < states; i++) {
        cout << i << ":";
        delta.push_back(transitions());
        for (int j = 0; j < alph_sz; j++) {
            int x;
            cout << "\t--" << (char)(j + 'a') << "--> ? ";
            cin >> x;
            delta[i].push_back(x);
        }
    }

    return Automaton(0, states, delta, accept);
}

int main(int argc, char *argv[]) {
    if (argc != 4) {
        fprintf(stderr, "Usage: %s states alph_sz word_sz\n", argv[0]);
        return -1;
    }

    int states = atoi(argv[1]);
    int alph_sz = atoi(argv[2]);
    int word_sz = atoi(argv[3]);


    // vector<Automaton> automatons = generate_automatons(states, alph_sz);

    // for (auto a: automatons) {
    //     a.dump();
    //     cout << "\n";
    // }

    fprintf(stderr, "Generating words...\n");
    vector<word> words = generate_words(word_sz, alph_sz);

    // for (auto w: words) {
    //     for (auto c: w) {
    //         cout << (char)(c + 'a');
    //     }
    //     cout << "\n";
    // }
    // fprintf(stderr, "Generating %d automatons...\n", my_power(alph_sz, alph_sz * states));
    // generate_automatons(states, alph_sz, words);

    Automaton aut = automaton_from_input();
    check_automaton(words, aut);
}