Skip to content

Commit d3c4e40

Browse files
committed
srm 150 div 1 500
1 parent b400c0a commit d3c4e40

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

StripePainter.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
18+
class StripePainter{
19+
public:
20+
int minStrokes(string s) {
21+
int n = s.size(), strokes = 0;
22+
string t(n, '#');
23+
int dp[55][55] = {0};
24+
for(int i = 0; i < n; i++)
25+
dp[i][i] = 1;
26+
for(int l = 2; l <= n; l++){
27+
for(int i = 0; i+l <= n; i++){
28+
int minv = 1000, j = i+l-1;
29+
for(int k = i; k < j; k++){
30+
int v = dp[i][k] + dp[k+1][j];
31+
if(s[i] == s[k+1]) v--;
32+
minv = min(v, minv);
33+
}
34+
dp[i][j] = minv;
35+
// cout<<"dp["<<i<<"]["<<j<<"] = "<<dp[i][j]<<endl;
36+
}
37+
}
38+
39+
strokes = dp[0][n-1];
40+
return strokes;
41+
}
42+
43+
// BEGIN CUT HERE
44+
public:
45+
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(); }
46+
private:
47+
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(); }
48+
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; } }
49+
void test_case_0() { string Arg0 = "RGBGR"; int Arg1 = 3; verify_case(0, Arg1, minStrokes(Arg0)); }
50+
void test_case_1() { string Arg0 = "RGRG"; int Arg1 = 3; verify_case(1, Arg1, minStrokes(Arg0)); }
51+
void test_case_2() { string Arg0 = "ABACADA"; int Arg1 = 4; verify_case(2, Arg1, minStrokes(Arg0)); }
52+
void test_case_3() { string Arg0 = "AABBCCDDCCBBAABBCCDD"; int Arg1 = 7; verify_case(3, Arg1, minStrokes(Arg0)); }
53+
void test_case_4() { string Arg0 = "BECBBDDEEBABDCADEAAEABCACBDBEECDEDEACACCBEDABEDADD"; int Arg1 = 26; verify_case(4, Arg1, minStrokes(Arg0)); }
54+
55+
// END CUT HERE
56+
57+
};
58+
59+
// BEGIN CUT HERE
60+
int main(){
61+
62+
StripePainter ___test;
63+
___test.run_test(-1);
64+
}
65+
// END CUT HERE

StripePainter.html

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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>
2+
Karel is a frustrated painter who works by day in an electrical repair
3+
shop. Inspired by the color-coded bands on resistors, he is painting
4+
a series of long, narrow canvases with bold colored stripes. However,
5+
Karel is lazy and wants to minimize the number of brush strokes it takes
6+
to paint each canvas.
7+
</p>
8+
9+
<p>
10+
Abbreviating each color to a single uppercase letter, Karel would
11+
write the stripe pattern red-green-blue-green-red as <tt>&quot;RGBGR&quot;</tt>
12+
(quotes added for clarity). It would take him three brush strokes to
13+
paint this pattern.
14+
The first stroke would cover the entire canvas in red (<tt>RRRRR</tt>).
15+
The second stroke would leave a band of red on either side and fill
16+
in the rest with green (<tt>RGGGR</tt>).
17+
The final brush stroke would fill in the blue stripe in the center
18+
(<tt>RGBGR</tt>).
19+
</p>
20+
21+
<p>
22+
Given a stripe pattern <b>stripes</b> as a string, calculate the minimum number of
23+
brush strokes required to paint that pattern.
24+
</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>StripePainter</td></tr><tr><td>Method:</td><td>minStrokes</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 minStrokes(string stripes)</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>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>The blank canvas is an ugly color and must not show through.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>stripes</b> will contain only uppercase letters ('A'-'Z', inclusive).</td></tr><tr><td align="center" valign="top">-</td><td><b>stripes</b> will contain between 1 and 50 characters, inclusive.</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;RGBGR&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Example from introduction.</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;RGRG&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">This example cannot be done in two strokes, even though there are only two colors.
25+
Suppose you tried to paint both red stripes in one stroke, followed by both green stripes
26+
in one stroke. Then the green stroke would cover up the second red stripe. If you tried
27+
to paint both green stripes first, followed the red stripes, then the red stroke would
28+
cover up the first green stripe.</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;ABACADA&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</pre></td></tr><tr><td><table><tr><td colspan="2">One long stroke in color 'A', followed by one stroke each in colors 'B', 'C', and 'D'.</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;AABBCCDDCCBBAABBCCDD&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 7</pre></td></tr><tr><td></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;BECBBDDEEBABDCADEAAEABCACBDBEECDEDEACACCBEDABEDADD&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: 26</pre></td></tr><tr><td></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)