-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution.cpp
68 lines (61 loc) · 1.97 KB
/
solution.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
/*
ll cache[60][4010];
ll solve(int start, int N, vector<int>& zero, vector<int>& one, vector<int>& accepting) {
if (N == 0) {
if (accepting[start]) {
return 1;
}
return 0;
}
if (cache[start][N] != -1) {
return cache[start][N];
}
ll res = 0;
res += solve(zero[start], N - 1, zero, one, accepting);
res %= MOD;
res += solve(one[start], N - 1, zero, one, accepting);
return cache[start][N] = res;
}
*/
int automata(vector<int> &zeroEdge, vector<int> &oneEdge, vector<int> &accepting, int start, int N) {
// memset(cache, -1, sizeof(cache));
int states = zeroEdge.size();
vector<int> acceptingState(states, 0);
for (auto acc: accepting) {
acceptingState[acc] = 1;
}
// return (int)solve(start, N, zeroEdge, oneEdge, acceptingState);
/**
* Time: O(N*K) where N = length, K = no of states
* Space: O(K)
* Iterative Dynamic Programming
*/
vector<ll> acceptingBefore(states, 0); // for length (i - 1)
vector<ll> acceptingCurrent(states, 0); // for length (i)
for (int i = 0; i < states; ++i) {
if (acceptingState[i]) {
acceptingCurrent[i] = 1; // for length 0, it is accepting
}
}
for (int len = 1; len <= N; ++len) {
acceptingBefore = acceptingCurrent;
for (int state = 0; state < states; ++state) {
acceptingCurrent[state] = (acceptingBefore[zeroEdge[state]] + acceptingBefore[oneEdge[state]]) % MOD;
}
}
// only consider strings accepted with start state and having length N
return (int)acceptingCurrent[start];
}
int main () {
vector<int> zeroEdges = {0, 2, 1};
vector<int> oneEdges = {1, 0, 2};
vector<int> accepting = {0};
int start = 0;
int length = 2;
cout << automata(zeroEdges, oneEdges, accepting, start, length) << endl;
return 0;
}