Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- string S;
- int k;
- int dp[105][105];
- bool cmp(string a, string b) {
- if(a.size() > b.size()) return false;
- if(a.size() < b.size()) return true;
- if(a < b) return true;
- if(a > b) return false;
- if(a == b) return true;
- return false;
- }
- int rec(int at, int k_left) {
- if(at == S.size() - 1 and k_left == k) {
- return 1;
- }
- if(at != -1 and dp[at][k_left] != -1) {
- return dp[at][k_left];
- }
- int result = 0;
- string tmp = "";
- for(int i = at + 1; i < S.size(); i++) {
- tmp += S[i];
- if(tmp[0] == '0' and tmp.size() >= 2) {
- continue;
- }
- if(cmp(tmp, "255")) {
- result += rec(i, k_left + 1);
- }
- else {
- break;
- }
- }
- if(at != -1) {
- dp[at][k_left] = result;
- }
- return result;
- }
- int main()
- {
- cin >> S >> k;
- for(int i = 0; i < 105; i++) {
- for(int j = 0; j < 105; j++) {
- dp[i][j] = -1;
- }
- }
- cout << rec(-1, 0) << endl;
- return 0;
- }
- // 1234567 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement