Skip to content

Commit 9f476ee

Browse files
committed
srm 631 div 2 250
1 parent abefc45 commit 9f476ee

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

TaroGrid.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
17+
class TaroGrid {
18+
public:
19+
int getNumber(vector <string> grid)
20+
{
21+
int bc = 0, wc = 0;
22+
int c = 1;
23+
int n = grid.size();
24+
for(int j = 0; j < n; j++){
25+
int i = 0;
26+
while(i+1 < n){
27+
int tc = 1;
28+
while(i+1 < n && grid[i][j] == grid[i+1][j]){
29+
i++;
30+
tc++;
31+
}
32+
i++;
33+
c = max(c, tc);
34+
}
35+
}
36+
return c;
37+
}
38+
39+
// BEGIN CUT HERE
40+
public:
41+
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(); }
42+
private:
43+
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(); }
44+
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; } }
45+
void test_case_0() { string Arr0[] = {"W"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(0, Arg1, getNumber(Arg0)); }
46+
void test_case_1() { string Arr0[] = {"WB",
47+
"BW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, getNumber(Arg0)); }
48+
void test_case_2() { string Arr0[] = {"BWW",
49+
"BBB",
50+
"BWB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(2, Arg1, getNumber(Arg0)); }
51+
void test_case_3() { string Arr0[] = {"BWBW",
52+
"BBWB",
53+
"WWWB",
54+
"BWWW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(3, Arg1, getNumber(Arg0)); }
55+
void test_case_4() { string Arr0[] = {"BWB",
56+
"BBW",
57+
"BWB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(4, Arg1, getNumber(Arg0)); }
58+
void test_case_5() { string Arr0[] = {"BBWWBBWW",
59+
"BBWWBBWW",
60+
"WWBBWWBB",
61+
"WWBBWWBB",
62+
"BBWWBBWW",
63+
"BBWWBBWW",
64+
"WWBBWWBB",
65+
"WWBBWWBB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(5, Arg1, getNumber(Arg0)); }
66+
67+
// END CUT HERE
68+
69+
};
70+
71+
// BEGIN CUT HERE
72+
int main(){
73+
74+
TaroGrid ___test;
75+
___test.run_test(-1);
76+
77+
}
78+
// END CUT HERE

TaroGrid.txt

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
PROBLEM STATEMENT
2+
3+
Cat Taro has a square grid with N rows and N columns.
4+
Each cell of the grid is painted either black or white.
5+
You are given a vector <string> grid which represents the current state of the grid.
6+
Each element of grid represents one row of the grid.
7+
In grid, the character 'W' represents a white cell, and the character 'B' represents a black cell.
8+
9+
10+
11+
12+
Taro wants to choose a set of consecutive cells that are in the same column and are painted in the same color.
13+
Return the largest number of cells he can choose.
14+
15+
16+
DEFINITION
17+
Class:TaroGrid
18+
Method:getNumber
19+
Parameters:vector <string>
20+
Returns:int
21+
Method signature:int getNumber(vector <string> grid)
22+
23+
24+
CONSTRAINTS
25+
-N will be between 1 and 50, inclusive.
26+
-grid will contain exactly N elements.
27+
-Each element of grid will contain exactly N characters.
28+
-Each character in grid will be 'W' or 'B'.
29+
30+
31+
EXAMPLES
32+
33+
0)
34+
{"W"}
35+
36+
Returns: 1
37+
38+
39+
40+
1)
41+
{"WB",
42+
"BW"}
43+
44+
Returns: 1
45+
46+
47+
48+
2)
49+
{"BWW",
50+
"BBB",
51+
"BWB"}
52+
53+
Returns: 3
54+
55+
He can choose the entire leftmost column (i.e., character 0 of each element of grid).
56+
57+
3)
58+
{"BWBW",
59+
"BBWB",
60+
"WWWB",
61+
"BWWW"}
62+
63+
Returns: 3
64+
65+
66+
67+
4)
68+
{"BWB",
69+
"BBW",
70+
"BWB"}
71+
72+
Returns: 3
73+
74+
75+
76+
5)
77+
{"BBWWBBWW",
78+
"BBWWBBWW",
79+
"WWBBWWBB",
80+
"WWBBWWBB",
81+
"BBWWBBWW",
82+
"BBWWBBWW",
83+
"WWBBWWBB",
84+
"WWBBWWBB"}
85+
86+
Returns: 2
87+
88+
Note that the chosen cells must be consecutive.

0 commit comments

Comments
 (0)