当前位置:网站首页>ACM. HJ70 矩阵乘法计算量估算 ●●
ACM. HJ70 矩阵乘法计算量估算 ●●
2022-06-24 23:40:00 【chenyfan_】
HJ70 矩阵乘法计算量估算 ●●
描述
矩阵乘法的运算量与矩阵乘法的顺序强相关。
例如:
A是一个50×10的矩阵,B是10×20的矩阵,C是20×5的矩阵
计算ABC有两种顺序:((AB)C)或者(A(BC)),前者需要计算15000次乘法,后者只需要3500次。
编写程序计算不同的计算顺序需要进行的乘法次数。
数据范围:矩阵个数: 1 ≤ n ≤ 15 1\le n\le 15 1≤n≤15,行列数: 1 ≤ r o w i , c o l i ≤ 100 1\le row_i,col_i\le 100 1≤rowi,coli≤100,保证给出的字符串表示的计算顺序唯一。
进阶:时间复杂度: O ( n ) O(n) O(n),空间复杂度: O ( n ) O(n) O(n)
输入描述:
输入多行,先输入要计算乘法的矩阵个数 n,每个矩阵的行数,列数,总共 2n 的数,最后输入要计算的法则;
计算的法则为一个字符串,仅由左右括号和大写字母(‘A’~‘Z’)组成,保证括号是匹配的且输入合法!
输出描述:
输出需要进行的乘法次数
示例
输入:
3
50 10
10 20
20 5
(A(BC))
输出:
3500
题解
1. 栈
首先要知道矩阵运算的过程和规律,
以 [ m , n ] [m, n] [m,n] 表示 m m m 行 n n n 列的矩阵,以 [ m , n ] ∗ [ n , p ] [m, n] * [n, p] [m,n]∗[n,p] 为例进行矩阵乘法规则说明:
- 第一个矩阵取一行,第二个矩阵取一列,计算时是对应相乘,有 n n n 次乘法。
- 还是第一个矩阵刚参加运算的那行,第二个矩阵的所有列(共 p 列),会有 n ∗ p n*p n∗p 次乘法
- 第一个矩阵的所有行(共 m m m 行)参加运算,共会有 n ∗ p ∗ m n*p*m n∗p∗m 次乘法运算。
- 得出 [ m , n ] ∗ [ n , p ] [m, n] * [n, p] [m,n]∗[n,p] 共会有 n ∗ p ∗ m n*p*m n∗p∗m 次乘法运算,运算后得到的矩阵为 [ m , p ] [m, p] [m,p]
因此,我们可以利用栈来进行判断和运算,遍历字符串,
- 当字符为左括号时,跳过;
- 当字符为字母时,转化为对应的矩阵大小加入栈内;
- 当字符为右括号时,取出栈顶的两个矩阵进行运算,并将运算后得到的矩阵大小压入栈中。
可知字符串的长度为 n + 2 * ( n - 1),n 为矩阵个数。
- 时间复杂度:O(N),遍历所有矩阵或规定计算顺序的字符串的代价,其中N为矩阵的数量
- 空间复杂度:O(N),存储矩阵数据的空间大小,和其他一些等代价的空间申请的大小

#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] 表示行
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){
// 字符串的长度为 n + 2*(n-1)
char ch;
cin >> ch;
if(ch == '('){
// 左括号不处理
continue;
}else if(ch == ')'){
// 右括号,对头两个矩阵进行操作
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]; // 计算次数为 a行 * a列 * b列
st.emplace(vector<int>{
a[0], b[1]}); // 将计算后的新矩阵大小压入栈内
}else{
st.emplace(m[ch-'A']); // 字母,使对应的矩阵大小入栈
}
}
cout << ans << endl;
}
return 0;
}
边栏推荐
- Unity archive system - file in JSON format
- 算力服务网络:一场多元融合的系统革命
- LINQ query (3)
- LeetCode 210:课程表 II (拓扑排序)
- 当人们用互联网式的思维和视角来看待产业互联网的时候,其实已陷入到了死胡同
- 给你讲懂 MVCC 续篇
- ERROR日志格式与注意点
- QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
- Getting started with unityshader - Surface Shader
- 1-6搭建Win7虚拟机环境
猜你喜欢

当他们在私域里,掌握了分寸感

It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets

UnityShader入门精要——表面着色器

I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating

Transformers Roberta如何添加tokens

【STL源码剖析】STL六大组件功能与运用(目录)

记一次beego通过go get命令后找不到bee.exe的坑

高速缓存Cache详解(西电考研向)

ProcessOn制作ER过程(自定义)

Is it out of reach to enter Ali as a tester? Here may be the answer you want
随机推荐
Of the seven levels of software testers, it is said that only 1% can achieve level 7
NPM package publishing tutorial
UnityShader入门精要——表面着色器
Squid 代理服务器之 ACL 访问控制
計網 | 【四 網絡層】知識點及例題
Kaggle 专利匹配比赛赛后总结
QT package the EXE file to solve the problem that "the program input point \u zdapvj cannot be located in the dynamic link library qt5cored.dll"
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
Leetcode 210: curriculum II (topological sorting)
The Oracle 11g RAC cluster database cannot be started due to directory permission errors
Internship: use of SVN
Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career
Advanced usage of groovy
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
数据库系统概论必背知识
Using qdomdocument to manipulate XML files in QT
left join on和 join on的区别
Enlightenment of using shadergraph to make edge fusion particle shader
目录权限错误导致 Oracle 11g rac 集群数据库无法启动的问题
PSQL column to row