forked from arshdeep/topcoder
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuttingBitString.cpp
More file actions
Latest commit
171 lines (147 loc) · 2.93 KB
/
Copy pathCuttingBitString.cpp
File metadata and controls
171 lines (147 loc) · 2.93 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<iostream>
#include<cstdio>
#include<vector>
#include<cassert>
#include<sstream>
#include<set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<time.h>
#include<stdlib.h>
#include<regex>
usingnamespacestd;
/* http://community.topcoder.com/stat?c=problem_statement&pm=12155 */
#defineSIZEOFARRAY(s) sizeof(s)/sizeof(s[0])
#defineREP(i,n) for(int i=0;i<(n);++i)
#defineLOOP(i,v,n) for(i=v;i<(n);++i)
#defineLLlonglong
#definememSet(m, v) memset(m, v, sizeof(m))
classCuttingBitString
{
map<string, int> memo;
boolcheck(string sub)
{
if (sub[0] == '0') return0;
longlong num = 1;
for (int idx = 1, len = sub.length(); idx < len; ++idx)
{
num <<= 1;
if (sub[idx] == '1')
num |= 1;
}
while (num % 5 == 0)
num /= 5;
return num == 1;
}
public:
intgetmin(string S)
{
map<string, int>::iterator itr = memo.find(S);
if (itr != memo.end())
return itr->second;
int res = INT_MAX;
for (int idx = 0, len = S.length(); idx < len; ++idx)
{
int tmp = 0;
if ( check(S.substr(0, idx + 1)) && ( (idx + 1) == len || S[idx + 1] == '1' ) )
{
string str = S.substr(idx + 1, len - idx - 1);
int min = getmin(str);
if (str.length() == 0)
tmp = 1;
elseif (min != -1)
tmp += min + 1;
}
if (tmp > 0)
res = std::min(tmp, res);
}
res = res == INT_MAX ? -1 : res;
memo.insert(make_pair(S, res));
return res;
}
};
voidTEST(string s, int expected)
{
clock_t start, end;
double cpu_time_used;
start = clock();
CuttingBitString cuttingBitString;
int result = cuttingBitString.getmin(s);
assert( expected == result );
end = clock();
cpu_time_used = ((double)(end - start));
cout << "Time taken : " << cpu_time_used << endl;
}
template <classT>
vector<T> convert(T *list, int n)
{
vector<T> ret;
for (int i = 0; i< n; ++i)
{
ret.push_back(list[i]);
}
return ret;
}
voidTest1()
{
TEST("101101101", 3 );
TEST("1111101", 1 );
TEST("00000", -1 );
TEST("110011011", 3 );
TEST("1000101011", -1 );
TEST("111011100110101100101110111", 5 );
TEST("11111011111101111111011111101111111111111101111111", 20 );
TEST("101101101101101101101101101101101101101101101101", 16 );
TEST("00010111001110100000101101001100010011111111100110", -1 );
TEST("000001111101", -1 );
TEST("1011111011011111011011111011011111011011111011111", 29 );
TEST("111101000010011001010100000010111110011", 3 );
TEST("101110011100011001110001111110110011100011111101", 7 );
TEST("100010101100011100100011000001001000100111101", 1 );
TEST("10111010010000111011011101", 1 );
TEST("1101100011010111001001101011011100010111011110101", 1 );
}
voidTest2()
{
}
voidTest3()
{
}
voidTest4()
{
}
voidTest5()
{
}
voidTest6()
{
}
voidTest7()
{
}
voidTest8()
{
}
voidTest9()
{
}
voidTest10()
{
}
intmain()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
Test10();
cout << "success";
getchar();
return0;
}