Skip to content

Commit 8d048c9

Browse files
committed
tci 02 round 2 500
1 parent 9725491 commit 8d048c9

File tree

2 files changed

+288
-0
lines changed

2 files changed

+288
-0
lines changed

MatArith.cpp

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <numeric>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef long long ll;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
typedef vector<long> vl;
18+
typedef vector<vl> vvl;
19+
map<char, vvl> M;
20+
21+
vvl operator+(vvl a, vvl b){
22+
if(a.empty() || b.empty()) return vvl();
23+
vvl r;
24+
int n = a.size(), m = a[0].size();
25+
if(b.size() != n || b[0].size() != m) return r;
26+
r = a;
27+
for(int i = 0; i < n; i++)
28+
for(int j = 0; j < m; j++){
29+
r[i][j] = a[i][j]+b[i][j];
30+
if(r[i][j] > INT_MAX || r[i][j] < INT_MIN) return vvl();
31+
}
32+
return r;
33+
}
34+
vvl operator*(vvl a, vvl b){
35+
if(a.empty() || b.empty()) return vvl();
36+
vvl r;
37+
int n = a.size(), m = a[0].size();
38+
if(b.size() != m) return r;
39+
int p = b[0].size();
40+
r.resize(n);
41+
for(int i = 0; i < n; i++){
42+
r[i].resize(p);
43+
fill(r[i].begin(), r[i].end(), 0);
44+
}
45+
for(int i = 0; i < n; i++)
46+
for(int j = 0; j < p; j++){
47+
for(int k = 0; k < m; k++){
48+
r[i][j] += a[i][k]*b[k][j];
49+
}
50+
if(r[i][j] > INT_MAX || r[i][j] < INT_MIN) return vvl();
51+
}
52+
53+
return r;
54+
}
55+
vvl to(vs a){
56+
if(a.empty()) return vvl();
57+
vvl aa;
58+
int n = a.size(), m = a[0].size();
59+
aa.resize(n);
60+
for(int i = 0; i < n; i++){
61+
stringstream ss(a[i]);
62+
ll k;
63+
while(ss>>k) aa[i].push_back(k);
64+
}
65+
return aa;
66+
}
67+
vs from(vvl a){
68+
if(a.empty()) return vs();
69+
vs aa;
70+
int n = a.size(), m = a[0].size();
71+
aa.resize(n);
72+
for(int i = 0; i < n; i++){
73+
string s;
74+
stringstream ss(s);
75+
for(int j = 0; j < m; j++)
76+
if(j == 0) ss<<a[i][j];
77+
else ss<<" "<<a[i][j];
78+
aa[i] = ss.str();
79+
// cout<<aa[i]<<endl;
80+
}
81+
return aa;
82+
}
83+
vvl cal(string s){
84+
if(s.empty()) return vvl();
85+
char op;
86+
if(s.size() == 1) return M[s[0]];
87+
if(s.size() == 3){
88+
op = s[1];
89+
vvl a = M[s[0]], b = M[s[2]];
90+
if(op == '+') return a+b;
91+
return a*b;
92+
}
93+
int p = -1;
94+
for(int i = 0; i < s.size(); i++) if(s[i] == '+') { p = i;}
95+
vvl a, b;
96+
if(p != -1){
97+
a = cal(s.substr(0, p)); b = cal(s.substr(p+1));
98+
return a+b;
99+
}
100+
else{
101+
int n = s.size();
102+
a = cal(s.substr(0, n-2)); b = cal(s.substr(n-1));
103+
return a*b;
104+
}
105+
}
106+
107+
class MatArith {
108+
public:
109+
vector <string> calculate(vs A, vs B, vs C, string eq)
110+
{
111+
vvl a = to(A), b = to(B), c = to(C);
112+
M['A'] = a; M['B'] = b; M['C'] = c;
113+
vvl v = cal(eq);
114+
//vvl v = a*b;
115+
for(int i = 0; i < v.size(); i++){
116+
for(int j = 0; j < v[i].size(); j++)
117+
cout<<v[i][j]<<" ";
118+
cout<<endl;
119+
}
120+
return from(v);
121+
}
122+
123+
// BEGIN CUT HERE
124+
public:
125+
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(); }
126+
private:
127+
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(); }
128+
void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
129+
void test_case_0() { string Arr0[] = {"1 2 3","2 3 4"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"1 2","3 4","5 6"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"1"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arg3 = "A*B"; string Arr4[] = { "22 28", "31 40" }; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(0, Arg4, calculate(Arg0, Arg1, Arg2, Arg3)); }
130+
void test_case_1() { string Arr0[] = {"1 2 3","2 3 4"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"1 2","3 4","5 6"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"1"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arg3 = "A+B+C"; string Arr4[] = { }; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(1, Arg4, calculate(Arg0, Arg1, Arg2, Arg3)); }
131+
void test_case_2() { string Arr0[] = {"3 5 7","5 4 3","-2 3 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"3"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"1 1 1","2 5 2","3 5 -3"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arg3 = "A+C"; string Arr4[] = { "4 6 8", "7 9 5", "1 8 -1" }; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(2, Arg4, calculate(Arg0, Arg1, Arg2, Arg3)); }
132+
void test_case_3() { string Arr0[] = {"10 0","0 0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arg3 = "A*A*A*A*A*A*A*A*A"; string Arr4[] = { "1000000000 0", "0 0" }; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(3, Arg4, calculate(Arg0, Arg1, Arg2, Arg3)); }
133+
void test_case_4() { string Arr0[] = {"10 0","0 0"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"0"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); string Arr2[] = {"0"}; vector <string> Arg2(Arr2, Arr2 + (sizeof(Arr2) / sizeof(Arr2[0]))); string Arg3 = "A*A*A*A*A*A*A*A*A*A"; string Arr4[] = { }; vector <string> Arg4(Arr4, Arr4 + (sizeof(Arr4) / sizeof(Arr4[0]))); verify_case(4, Arg4, calculate(Arg0, Arg1, Arg2, Arg3)); }
134+
135+
// END CUT HERE
136+
137+
};
138+
139+
// BEGIN CUT HERE
140+
int main()
141+
{
142+
MatArith ___test;
143+
___test.run_test(-1);
144+
}
145+
// END CUT HERE

MatArith.txt

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
PROBLEM STATEMENT
2+
Let an R by C (also notated as RxC) matrix mean a matrix with R rows and C columns. Also, for x between 1 and R, inclusive, and y between 1 and C inclusive, let element (x,y) represent the element at the xth row and yth column (both 1-indexed) of a given R by C matrix.
3+
Matrix multiplication works as follows:
4+
For an L by M matrix A and an M by N matrix B, define an L by N matrix P such that A * B = P, and each element (x,y) of matrix P is equal to the sum of the product of each element of the xth row of A with the equivalently indexed element of the yth column of B.
5+
For example, say we have a 4x2 matrix A and a 2x3 matrix B:
6+
7+
A={{ a1, a2 },
8+
{ a3, a4 },
9+
{ a5, a6 },
10+
{ a7, a8 }}
11+
12+
B={{ b1, b2, b3 },
13+
{ b4, b5, b6 }}
14+
15+
Then 4x3 matrix P = A * B =
16+
{{ b1*a1+b4*a2, b2*a1+b5*a2, b3*a1+b6*a2 },
17+
{ b1*a3+b4*a4, b2*a3+b5*a4, b3*a3+b6*a4 },
18+
{ b1*a5+b4*a6, b2*a5+b5*a6, b3*a5+b6*a6 },
19+
{ b1*a7+b4*a8, b2*a7+b5*a8, b3*a7+b6*a8 }}
20+
21+
Matrix addition works as follows:
22+
For an L by M matrix A and an L by M matrix B, define an L by M matrix S such that A + B = S, and each element (x,y) of matrix C is equal to the sum of the two elements (x,y) in A and B.
23+
For example, say we have a 2x3 matrix A and a 2x3 matrix B:
24+
25+
A={{ a1, a2, a3 },
26+
{ a4, a5, a6 }}
27+
28+
B={{ b1, b2, b3 },
29+
{ b4, b5, b6 }}
30+
31+
Then 2x3 matrix S = A + B =
32+
{{ a1+b1, a2+b2, a3+b3 }
33+
{ a4+b4, a5+b5, a6+b6 }}
34+
35+
36+
Write a method multiply which takes three vector <string> inputs representing matrix A, matrix B, and matrix C. Each element will be a string representing a row of that matrix as follows:
37+
"I1 I2 I3 I4 I5 ... IN" (quotes added for clarity)
38+
where each I is an integer.
39+
40+
The fourth argument is a string representing an equation between A, B, and C.
41+
Return the vector <string> represention of the solution matrix to the equation, in the same form as the inputs.
42+
43+
The equation follows the standard order of operations. That is, first do any multiplications, going left to right. Then do any additions, going left to right. After each multiplication or addition, put an intermediate matrix in place of the two argument matrices. For example (quotes added for clarity):
44+
"A*B+C*A"
45+
You would first multiply A*B, and replace those two matrices with an intermediate matrix M:
46+
"M+C*A"
47+
You would then multiply C*A, and replace those two matrices with an intermediate matrix N:
48+
"M+N"
49+
You would finally add M+N, returning the final result.
50+
If the equation is not valid for the given matrices, return an empty String[]. The equation is not valid if:
51+
*at any point, two matrices (A, B, C, or any intermediates) must be multiplied, in which the number of rows in the second matrix is not equal to the number of columns in the first matrix.
52+
*at any point, two matrices (A, B, C, or any intermediates) must be added, but do not have the same dimensions.
53+
*at any point, one of the intermediate matrices contains a value that is greater than 2147483647 or less than -2147483648.
54+
In summary, given the 3 matrices and an equation involving those three matrices, return the resulting matrix in the described vector <string> format, or an empty vector <string> if the equation is not valid for the given matrices.
55+
56+
57+
DEFINITION
58+
Class:MatArith
59+
Method:calculate
60+
Parameters:vector <string>, vector <string>, vector <string>, string
61+
Returns:vector <string>
62+
Method signature:vector <string> calculate(vector <string> A, vector <string> B, vector <string> C, string equation)
63+
64+
65+
NOTES
66+
-The return must be formatted exactly as the inputs. That means no leading/trailing or extra spaces.
67+
68+
69+
CONSTRAINTS
70+
-Each vector <string> will contain 1 to 10 element, inclusive.
71+
-Each element of A, B and C will contain between 1 and 50 characters, inclusive.
72+
-All elements of A, B and C will be a space-delimited list of integers, with one space between integers, and no leading or trailing spaces.
73+
-All elements of A, B and C will contain only the numbers [0-9] inclusive, the negative sign ('-') and spaces.
74+
-The number of integers represented by each element of A, B and C will be between 1 and 10, inclusive.
75+
-The number of integers represented by each element of A will be the same as the number of integers represented by every other element of A.
76+
-The number of integers represented by each element of B will be the same as the number of integers represented by every other element of B.
77+
-The number of integers represented by each element of C will be the same as the number of integers represented by every other element of C.
78+
-Each integer represented in A, B or C will be between -10 and 10, inclusive.
79+
-Each integer represented in A, B or C will be a correctly formatted integer, without leading zeros.
80+
-equation will contain between 1 and 49 characters, inclusive.
81+
-equation can only contain the capital letters 'A', 'B', and 'C', and the two operands '+' and '*'.
82+
-The first and last characters of equation will be letters, and each pair of consecutive letters in equations will have exactly one operand between them.
83+
84+
85+
EXAMPLES
86+
87+
0)
88+
{"1 2 3","2 3 4"}
89+
{"1 2","3 4","5 6"}
90+
{"1"}
91+
"A*B"
92+
93+
Returns: { "22 28", "31 40" }
94+
95+
A*B={{ 1*1+2*3+3*5, 1*2,2*4,3*6 },
96+
{ 2*1+3*3+4*5, 2*2+3*4+4*6 }}
97+
98+
99+
1)
100+
{"1 2 3","2 3 4"}
101+
{"1 2","3 4","5 6"}
102+
{"1"}
103+
"A+B+C"
104+
105+
Returns: { }
106+
107+
A+B is calculated first, but the two matrices do not have the same dimensions.
108+
109+
110+
2)
111+
{"3 5 7","5 4 3","-2 3 2"}
112+
{"3"}
113+
{"1 1 1","2 5 2","3 5 -3"}
114+
"A+C"
115+
116+
Returns: { "4 6 8", "7 9 5", "1 8 -1" }
117+
118+
119+
A+C={{ 3+1, 5+1, 7+1 },
120+
{ 2+5, 5+4, 2+3 },
121+
{ -2+3, 3+5, 2+-3 }}
122+
123+
124+
125+
126+
3)
127+
{"10 0","0 0"}
128+
{"0"}
129+
{"0"}
130+
"A*A*A*A*A*A*A*A*A"
131+
132+
Returns: { "1000000000 0", "0 0" }
133+
134+
4)
135+
{"10 0","0 0"}
136+
{"0"}
137+
{"0"}
138+
"A*A*A*A*A*A*A*A*A*A"
139+
140+
Returns: { }
141+
142+
An intermediate value (10,000,000,000) is greater than 2,147,483,647.
143+

0 commit comments

Comments
 (0)