UVa 442 (栈) Matrix Chain Multiplication
题意:
给出一个矩阵表达式,计算总的乘法次数。
分析:
基本的数学知识:一个m×n的矩阵A和n×s的矩阵B,计算AB的乘法次数为m×n×s。只有A的列数和B的行数相等时,两个矩阵才能进行乘法运算。
表达式的处理:可以用一个栈来存储,遇到字母入栈,遇到右括号将栈顶两个元素出栈,然后将乘积入栈。
#include <cstdio>
#include <cstring> const int maxn = ;
int n;
char s[]; struct Matrix
{
int n, m;
Matrix(int n=, int m=):n(n), m(m) {}
int times(const Matrix& rhs) const
{
if(m == rhs.n) return n * m * rhs.m;
return -;
}
Matrix operator * (const Matrix& rhs) const
{ return Matrix(n, rhs.m); }
}mat[maxn], stack[maxn]; int ID(char c) { return c - 'A'; } int main()
{
//freopen("in", "r", stdin);
scanf("%d", &n);
getchar();
for(int i = ; i < n; ++i)
{
int n, m;
char name;
scanf("%c %d %d", &name, &n, &m);
getchar();
mat[ID(name)] = Matrix(n, m);
} while(scanf("%s", s) == )
{
int l = strlen(s);
int ans = , p = , ok = ;
for(int i = ; i < l; ++i)
{
if(s[i] == '(') continue;
else if(s[i] == ')')
{
Matrix B = stack[--p];
Matrix A = stack[--p];
int t = A.times(B);
if(t != -)
{
ans += t;
stack[p++] = A * B;
}
else { ok = ; break; }
}
else
{
stack[p++] = mat[ID(s[i])];
}
} if(ok) printf("%d\n", ans);
else puts("error");
} return ;
}
代码君
UVa 442 (栈) Matrix Chain Multiplication的更多相关文章
- 【例题 6-3 UVA - 442】Matrix Chain Multiplication
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用栈来处理一下表达式就好. 因为括号是一定匹配的.所以简单很多. ab x bc会做abc次乘法. [代码] #include< ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试 得到的经验有以下几点: 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习 有时候代码不对,得到的结果却是对的(之后总结以下常见 ...
- UVA——442 Matrix Chain Multiplication
442 Matrix Chain MultiplicationSuppose you have to evaluate an expression like A*B*C*D*E where A,B,C ...
- Matrix Chain Multiplication(表达式求值用栈操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...
- ACM学习历程——UVA442 Matrix Chain Multiplication(栈)
Description Matrix Chain Multiplication Matrix Chain Multiplication Suppose you have to evaluate ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- Matrix Chain Multiplication[HDU1082]
Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- Review PHP设计模式之——单例模式
单例模式: class Single { private static $_instance; private function __construct(){ //define method as p ...
- MacOS10.9获取Android源码不完全笔记(2014)
第一步:安装Macports 这个我就不叙述了,网上有无数教程 第二步:创建一个磁盘镜像 1.打开磁盘工具,然后: 第三步:使用Macport安装编译环境 1.打开终端输入以下内容 sudo port ...
- js 使用技巧 - [近几年工作中的经验总结的技巧]
1.如果 ajax 返回单一的 json 格式,接收方需要这样再格式化一下赋值: var str = eval("(" + msg + ")"); 开发引用: ...
- Unity3d插件iTween的使用
iTween.cs 下载地址:http://pan.ceeger.com/viewfile.php?file_id=1830&file_key=0UJAymOJ 版本为2.0.43 一.iTw ...
- 【BZOJ 1202】 [HNOI2005]狡猾的商人
Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...
- To change the sharepoint CA port
Set-SPCentralAdministration -Port <port number> to fix the error: Got this error: Failed to re ...
- 使用pd设计表的 多对多的中间表的设计方式, 有图有真相
设计多对多表时解决重复问题 目前流行两种设计方式: 方式一 是在中间表中建一个单独的id主键, 与业务表关联的键设置为unique唯一; 干事二: 联合主键的方式, 该方式中间表不会有与业务表无关的主 ...
- 2017年iOS应用将强制使用HTTPS安全加密-b
6月14日,WWDC 2016苹果开发者大会上,苹果在讲解全新的iOS10中提到了数据安全这一方面,并且苹果宣布iOS应用将从2017年1月起启用名为App Transport Security的安全 ...
- 15个带示例的jQuery滚动条插件
1.NiceScroll:可用于桌面.移动与触摸设备的jQuery滚动插件 NiceScroll是一个jQuery插件(since 1.5),它有着类似于ios/移动设备的样式.它支持Div.iFra ...
- 【高斯消元】Poj 1222:EXTENDED LIGHTS OUT
Description In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each ...