-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStripePainter.cpp
More file actions
150 lines (116 loc) · 2.44 KB
/
StripePainter.cpp
File metadata and controls
150 lines (116 loc) · 2.44 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
#include <iostream>
#include <cstdio>
#include <vector>
#include <cassert>
#include <sstream>
#include <set>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <time.h>
using namespace std;
/* StripePainter https://www.topcoder.com/stat?c=problem_statement&pm=1215&rd=4555 */
class StripePainter
{
private:
std::unordered_map<string, int> memo;
public:
int minStrokesRec(string stripes);
int minStrokesDP(string stripes);
};
int StripePainter::minStrokesDP(string stripes)
{
int len = stripes.length();
vector <vector<int> > dp(len, vector<int>(len) );
for (int idx = 0; idx < len; ++idx)
{
dp[idx][idx] = 1;
}
for (int gap = 1; gap < len; ++gap)
{
for (int i = 0, j = gap; j < len; ++i, ++j)
{
int min = INT_MAX;
for (int k = i; k < j; ++k)
{
int d = stripes[i] == stripes[j];
min = std::min( min, dp[i][k] + dp[k + 1][j] - d );
}
dp[i][j] = min;
}
}
return dp[0][len - 1];
}
int StripePainter::minStrokesRec(string stripes)
{
int len = stripes.length();
if (len == 1) return 1;
unordered_map<string, int>::iterator itr = memo.find(stripes);
if (itr != memo.end())
return itr->second;
int min = INT_MAX;
for (int k = 1; k < len; ++k)
{
string s1 = stripes.substr(0, k);
string s2 = stripes.substr(k, len - k);
int t1 = minStrokesRec(s1);
int t2 = minStrokesRec(s2);
if ( !memo.count(s1) )
memo.insert( std::make_pair(s1, t1) );
if ( !memo.count(s2) )
memo.insert( std::make_pair(s2, t2) );
int d = stripes[0] == stripes[len - 1] ? 1 : 0;
min = std::min(min, t1 + t2 - d);
}
return min;
}
void TEST( string stripes, int expected )
{
clock_t start, end;
double cpu_time_used;
start = clock();
StripePainter stripePainter;
int result = stripePainter.minStrokesDP(stripes);
end = clock();
cpu_time_used = ((double) (end - start));
cout<<"Time taken : "<<cpu_time_used<<endl;
assert( result == expected );
}
void Test1()
{
TEST( "RGBGR", 3);
}
void Test2()
{
TEST( "RGBG", 3);
}
void Test3()
{
TEST( "ABACADA", 4);
}
void Test4()
{
TEST( "AABBCCDDCCBBAABBCCDD", 7);
}
void Test5()
{
TEST( "BECBBDDEEBABDCADEAAEABCACBDBEECDEDEACACCBEDABEDADD", 26);
}
void Test6()
{
TEST( "ABABABABABABABABABABABABABABABABABABABABABABABABAB", 26);
TEST( "DEJJEDAIJHBFCEGHFEADCJAIIGKBF", 21);
TEST( "ABACDBABDFDFJDFDFBCCABDBABABDBBCBCDBABCBJIEDF", 27);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
cout<<"success";
getchar();
return 0;
}