当前位置:网站首页>ACM. Hj70 matrix multiplication calculation amount estimation ●●
ACM. Hj70 matrix multiplication calculation amount estimation ●●
2022-06-25 02:53:00 【chenyfan_】
HJ70 Matrix multiplication calculation amount estimation ●●
describe
The computation of matrix multiplication is strongly related to the order of matrix multiplication .
for example :
A It's a 50×10 Matrix ,B yes 10×20 Matrix ,C yes 20×5 Matrix
Calculation ABC There are two orders :((AB)C) perhaps (A(BC)), The former needs to calculate 15000 Times multiplication , The latter only needs 3500 Time .
Write a program to calculate the multiplication times required for different calculation sequences .
Data range : The number of matrices : 1 ≤ n ≤ 15 1\le n\le 15 1≤n≤15, Number of rows and columns : 1 ≤ r o w i , c o l i ≤ 100 1\le row_i,col_i\le 100 1≤rowi,coli≤100, Ensure that the calculation order of the given string representation is unique .
Advanced : Time complexity : O ( n ) O(n) O(n), Spatial complexity : O ( n ) O(n) O(n)
Input description :
Input multiple lines , First enter the number of matrices to calculate the multiplication n, The number of rows per matrix , Number of columns , in total 2n Number of numbers , Finally, enter the rule to calculate ;
The calculation rule is a string , It consists only of left and right parentheses and capital letters (‘A’~‘Z’) form , Make sure the parentheses match and the input is legal !
Output description :
Output the number of multiplications to be performed
Example
Input :
3
50 10
10 20
20 5
(A(BC))
Output :
3500
Answer key
1. Stack
First of all, we should know the process and law of matrix operation ,
With [ m , n ] [m, n] [m,n] Express m m m That's ok n n n Columns of the matrix , With [ m , n ] ∗ [ n , p ] [m, n] * [n, p] [m,n]∗[n,p] As an example, the matrix multiplication rules are explained :
- The first matrix takes a row , The second matrix takes a column , The calculation is corresponding multiplication , Yes n n n Times multiplication .
- It's the row where the first matrix just participated in the operation , All the columns of the second matrix ( common p Column ), There will be n ∗ p n*p n∗p Times multiplication
- All rows of the first matrix ( common m m m That's ok ) Participate in operation , Co existing n ∗ p ∗ m n*p*m n∗p∗m Secondary multiplication .
- obtain [ m , n ] ∗ [ n , p ] [m, n] * [n, p] [m,n]∗[n,p] Co existing n ∗ p ∗ m n*p*m n∗p∗m Secondary multiplication , The matrix obtained after the operation is [ m , p ] [m, p] [m,p]
therefore , We can use stack to judge and calculate , Traversal string ,
- When the character is an open parenthesis , skip ;
- When the character is a letter , Convert to the corresponding matrix size and add it to the stack ;
- When the character is a closing bracket , Take out the two matrices at the top of the stack for operation , And the size of the matrix obtained after the operation is pushed onto the stack .
The length of the string is n + 2 * ( n - 1),n Is the number of matrices .
- Time complexity :O(N), The cost of traversing all matrices or strings that specify the order of computation , among N Is the number of matrices
- Spatial complexity :O(N), The size of the space in which the matrix data is stored , And some other equal cost space applications

#include <iostream>
#include <vector>
#include <map>
#include <stack>
using namespace std;
int main(){
int n;
while(cin >> n){
vector<vector<int>> m(n, vector<int>(2, 0)); // m[i][0] Said line
for(int i = 0; i < n; ++i){
cin >> m[i][0] >> m[i][1];
}
stack<vector<int>> st;
int ans = 0;
for(int i = 0; i < 3*n-2; ++i){
// The length of the string is n + 2*(n-1)
char ch;
cin >> ch;
if(ch == '('){
// The left parenthesis does not handle
continue;
}else if(ch == ')'){
// Right bracket , Operate on the first two matrices
vector<int> a(2, 0);
vector<int> b(2, 0);
b = st.top(); // a * b
st.pop();
a = st.top();
st.pop();
ans += a[0] * a[1] * b[1]; // The number of calculations is a That's ok * a Column * b Column
st.emplace(vector<int>{
a[0], b[1]}); // Push the calculated new matrix size onto the stack
}else{
st.emplace(m[ch-'A']); // Letter , Stack the corresponding matrix size
}
}
cout << ans << endl;
}
return 0;
}
边栏推荐
- Add in cmakelists_ Definitions() function
- mysql学习笔记--单张表上的增删改查
- vie的刷新机制
- Is it out of reach to enter Ali as a tester? Here may be the answer you want
- 给你讲懂 MVCC 续篇
- AOSP ~ default attribute value
- Leecode learning notes - the shortest path for a robot to reach its destination
- 分布式事务解决方案和代码落地
- 数组-一口气冲完快慢指针
- Using qdomdocument to manipulate XML files in QT
猜你喜欢

PyTorch学习笔记(七)------------------ Vision Transformer

Copilot免费时代结束!学生党和热门开源项目维护者可白嫖

Unity存档系统——Json格式的文件

好用的字典-defaultdict

14 BS object Node name Name attrs string get node name attribute content

数组-一口气冲完快慢指针

Pit entry machine learning: I. Introduction

Getting started with unityshader - Surface Shader

AI服装生成,帮你完成服装设计的最后一步

計網 | 【四 網絡層】知識點及例題
随机推荐
ACM. HJ75 公共子串计算 ●●
doak-cms 文章管理系统 推荐
automated testing
使用XXL-JOB自定义任务并调度
Can the polardb database be connected to the data source through MySQL
Internship: use of SVN
Advanced usage of groovy
Uncaught Error: [About] is not a <Route> component. All component children of <Routes> must be a <Ro
Processon producer process (customized)
Go synchronization waiting group
Charles 抓包工具
PSQL column to row
AOSP ~ 默认属性值
调用系统函数安全方案
mysql学习笔记--单张表上的增删改查
How transformers Roberta adds tokens
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
电脑端微信用户图片DAT格式解码为图片(TK版)
打新债是不是骗局 开户是安全的吗
给你讲懂 MVCC 续篇