Untitled
unknown
c_cpp
a year ago
1.0 kB
6
Indexable
const int N = 1e5 + 1, MOD = 1e9 + 7; int next_idx[N][2]; int mem[N][2]; class Solution { int go(int i, bool found_one, const string &binary) { if (i == -1) return 0; int &ret = mem[i][found_one]; if (~ret) return ret; found_one |= binary[i] == '1'; ret = found_one; if (found_one || binary[i] == '1') ret += go(next_idx[i][0], found_one, binary); ret += go(next_idx[i][1], found_one, binary); ret %= MOD; return ret; } public: int numberOfUniqueGoodSubsequences(string binary) { int last_0 = -1, last_1 = -1; for (int i = binary.size() - 1; i >= 0; --i) { next_idx[i][0] = last_0; next_idx[i][1] = last_1; if (binary[i] == '1') { last_1 = i; } else { last_0 = i; } } memset(mem, -1, sizeof mem); return go(0, false, binary) + (last_0 != -1); } };
Editor is loading...
Leave a Comment