Skip to content

Commit 1f8b2cd

Browse files
committed
srm 555 div 1 250
1 parent fcffb1a commit 1f8b2cd

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

CuttingBitString.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

CuttingBitString.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>We are in a distant future.
2+
After the downfall of mankind, the Earth is now ruled by fairies.
3+
The "Turing game Online" website is hot among fairies right now.
4+
On this website, everyone can play the programming puzzle "Turing game".</p>
5+
<br></br>
6+
<p>Fairies love powers of 5, that is, the numbers 1, 5, 25, 125, 625, and so on.
7+
In the Turing game, the player is given a string of bits (zeros and ones).
8+
The ideal situation is when the string is represents a power of 5 in binary, with no leading zeros.
9+
If that is not the case, the fairy player tries to cut the given string into pieces, each piece being a binary representation of a power of 5, with no leading zeros.
10+
Of course, it may be the case that even this is impossible.
11+
In that case, the fairy player becomes depressed, and bad things happen when a fairy gets depressed.
12+
You, as one of the surviving humans, are in charge of checking the bit strings to prevent the bad things from happening.</p>
13+
<br></br>
14+
<p>You are given a string <b>S</b> that consists of characters '0' and '1' only.
15+
<b>S</b> represents the string given to a player of the Turing game.
16+
Return the smallest positive integer K such that it is possible to cut <b>S</b> into K pieces, each of them being a power of 5.
17+
If there is no such K, return -1 instead.</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>CuttingBitString</td></tr><tr><td>Method:</td><td>getmin</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getmin(string S)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>S</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>S</b> will be either '0' or '1'.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;101101101&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2"><p>We can split the given string into three "101"s.</p>
18+
<p>Note that "101" is 5 in binary.</p></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;1111101&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1</pre></td></tr><tr><td><table><tr><td colspan="2"><p>"1111101" is 5^3.</p></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;00000&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2"><p>0 is not a power of 5.</p></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;110011011&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2"><p>Split it into "11001", "101" and "1".</p></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;1000101011&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: -1</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;111011100110101100101110111&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 5</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)