|
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <queue> |
| 8 | +#include <cstring> |
| 9 | +#include <climits> |
| 10 | +#include <cstdio> |
| 11 | +using namespace std; |
| 12 | +typedef pair<int,int> pi; |
| 13 | +typedef set<int> si; |
| 14 | +typedef vector<int> vi; |
| 15 | +typedef vector<vi> vvi; |
| 16 | +typedef vector<string> vs; |
| 17 | +typedef long long ll; |
| 18 | +#define sz size() |
| 19 | +#define mp make_pair |
| 20 | +#define pb push_back |
| 21 | +#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b) |
| 22 | +#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b] |
| 23 | +#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b) |
| 24 | +#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b] |
| 25 | +#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b) |
| 26 | +#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b] |
| 27 | +#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b) |
| 28 | +#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b] |
| 29 | +#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b) |
| 30 | +#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b] |
| 31 | +#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b) |
| 32 | +#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b] |
| 33 | +#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b] |
| 34 | + |
| 35 | +class CuttingBitString{ |
| 36 | + bool isPower(ll s){ |
| 37 | + if(s == 1) return true; |
| 38 | + if(s == 0) return false; |
| 39 | + return s%5 == 0 && isPower(s/5); |
| 40 | + } |
| 41 | + bool isPower(string s){ |
| 42 | + int i = 0; |
| 43 | + if(s[0] == '0') return false; |
| 44 | + ll a = stoll(s, NULL, 2); |
| 45 | + if(a == 1) return true; |
| 46 | + if(a == 0) return false; |
| 47 | + return a%5 == 0 && isPower(a/5); |
| 48 | + } |
| 49 | + |
| 50 | + public: |
| 51 | + int getmin(string s){ |
| 52 | + |
| 53 | + int n = s.size(); |
| 54 | + |
| 55 | + int dp[55] = {0}; |
| 56 | + fi(n){ |
| 57 | + string t = s.substr(0, i+1); |
| 58 | + |
| 59 | + if(isPower(t)){ |
| 60 | + dp[i] = 1; |
| 61 | + continue; |
| 62 | + } |
| 63 | + int minv = 1000; |
| 64 | + fj(i){ |
| 65 | + if(dp[j] == 0 || !isPower(s.substr(j+1, i-j))) continue; |
| 66 | + minv = min(minv, dp[j]+1); |
| 67 | + } |
| 68 | + if(minv < 1000) dp[i] = minv; |
| 69 | + } |
| 70 | + return dp[n-1] == 0 ? -1 : dp[n-1]; |
| 71 | + } |
| 72 | + |
| 73 | +// BEGIN CUT HERE |
| 74 | + public: |
| 75 | + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } |
| 76 | + private: |
| 77 | + template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
| 78 | + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
| 79 | + void test_case_0() { string Arg0 = "101101101"; int Arg1 = 3; verify_case(0, Arg1, getmin(Arg0)); } |
| 80 | + void test_case_1() { string Arg0 = "1111101"; int Arg1 = 1; verify_case(1, Arg1, getmin(Arg0)); } |
| 81 | + void test_case_2() { string Arg0 = "00000"; int Arg1 = -1; verify_case(2, Arg1, getmin(Arg0)); } |
| 82 | + void test_case_3() { string Arg0 = "110011011"; int Arg1 = 3; verify_case(3, Arg1, getmin(Arg0)); } |
| 83 | + void test_case_4() { string Arg0 = "1000101011"; int Arg1 = -1; verify_case(4, Arg1, getmin(Arg0)); } |
| 84 | + void test_case_5() { string Arg0 = "111011100110101100101110111"; int Arg1 = 5; verify_case(5, Arg1, getmin(Arg0)); } |
| 85 | + |
| 86 | +// END CUT HERE |
| 87 | + |
| 88 | + }; |
| 89 | + |
| 90 | + // BEGIN CUT HERE |
| 91 | + int main() |
| 92 | + { |
| 93 | + CuttingBitString ___test; |
| 94 | + ___test.run_test(-1); |
| 95 | + } |
| 96 | + // END CUT HERE |
0 commit comments