-
Notifications
You must be signed in to change notification settings - Fork 359
/
s1.cpp
30 lines (30 loc) · 834 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/restore-the-array/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(N)
class Solution {
typedef long long LL;
public:
int numberOfArrays(string s, int k) {
if (s[0] - '0' > k) return 0;
int cnt = 0, tmp = k;
while (tmp) {
tmp /= 10;
++cnt;
}
int N = s.size(), mod = 1e9+7;
vector<int> dp(N + 1);
dp[0] = dp[1] = 1;
for (int i = 2; i <= N; ++i) {
LL p = 1, n = 0;
for (int j = i - 1; j >= 0; --j) {
n += (s[j] - '0') * p;
p *= 10;
if (n > k || i - j > cnt) break;
if (n == 0 || s[j] == '0') continue;
dp[i] = (dp[i] + dp[j]) % mod;
}
}
return dp[N];
}
};