例题6-3 Matrix Chain Multiplication ,Uva 442
这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试
得到的经验有以下几点:
- 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习
- 有时候代码不对,得到的结果却是对的(之后总结以下常见错误)
- 能用结构体,就别用数组,容易出错(暂时还不知道为什么)=>现在知道申请的数组空间在运行期间被释放,除非用malloc去申请数组
- 代码要规范,空格该有就要有
- 有些不规范表达式,不同编译器出现不同结果,注意应避免使用这类语句
像这道题主要坑在了第三点上,以后要注意避免
以下是AC代码
第一次完成时间(大于2小时)
#include <cstdio>
#include <stack>
#include <cstring>
#include <cctype>
const int MAXN=+;
using namespace std;
char exps[MAXN];
struct Matrix {
int a, b;
Matrix(int a = ,int b = ):a(a), b(b) {}
}m[];
stack<Matrix> s;
int main(){
#ifdef DEBUG
freopen("6.3.in","r",stdin);
#endif
int n;
scanf("%d\r",&n);
for(int i=;i<n;i++){
char s0[];
char c;
scanf("%c ",&c);
s0[]=c;
scanf("%d %d\r\n",&m[s0[] -'A'].a, &m[s0[] -'A'].b);
//printf("%c %d %d\r\n",s0[0] , m[s0[0] -'A'].a , m[s0[0]-'A'].b);
}
while(scanf("%s",exps)==){
int sum=;
int len=strlen(exps);
int ok=;
for(int i=;i<len;i++){
if(isalpha(exps[i])){
s.push(m[exps[i]-'A']);
// printf("push %d %d \n",m[exps[i]-'A'].a,m[exps[i]-'A'].b);
}
else if(exps[i]==')'){
Matrix m2 = s.top(); s.pop();
// printf("pop %d %d \n", m2.a, m2.b);
Matrix m1 = s.top(); s.pop();
// printf("pop %d %d \n", m1.a, m1.b);
if(m1.b != m2.a){ok=;break;}
sum+= m1.a * m1.b * m2.b;
s.push(Matrix(m1.a, m2.b));
// printf("push %d %d \n",m1.a, m2.b,);
}
}
if(ok)printf("%d\n",sum);
else printf("error\n");
}
return ;
}
第二次练习代码(完成时间约1小时)
//UVa 442,Matrix Chain Multiplication
//Example:6-3
//Author:wzh
//Date: 2016.8.26
//Version 2 #include <cstdio>
#include <cstring>
#include <cctype>
#include <stack>
#include <cstdlib>
using namespace std;
#define maxn 30
int s[maxn][];
char buf[];
int main(){
#ifdef D
freopen("442.in","r",stdin);
#endif
int n;
scanf("%d ",&n);
for(int i=;i<n;i++){
char c;int a,b;
scanf("%c",&c);
scanf("%d%d ",&s[c-'A'][],&s[c-'A'][]);
//printf("%c %d %d\n",c,s[c-'A'][0],s[c-'A'][1]);
} while(fgets(buf,,stdin)){
int len=strlen(buf);
stack<int*> sk;
int sum=;
int ok=;
for(int i=;i<len;i++){
if(buf[i]==')'){
int *a,*b,*c;
b=sk.top();sk.pop();
//printf("pop%d %d\n",b[0],b[1]);
a=sk.top();sk.pop();
// printf("pop%d %d\n",a[0],a[1]);
if(a[]==b[]){
sum+=(a[]*a[]*b[]);
c=(int*)malloc(sizeof(int)*);
c[]=a[];
c[]=b[];
sk.push(c);
// printf("push*%d %d\n",c[0],c[1]);
}
else{
ok=;
break;
}
}
else if(isalpha(buf[i])){
sk.push(s[buf[i]-'A']);
//printf("push%d %d\n",s[buf[i]-'A'][0],s[buf[i]-'A'][1]);
}
}
if(ok)printf("%d\n",sum);
else printf("error\n");
}
return ;
}
例题6-3 Matrix Chain Multiplication ,Uva 442的更多相关文章
- Matrix Chain Multiplication UVA - 442
Suppose you have to evaluate an expression like ABCDE where A,B,C,D and E are matrices. Since matrix ...
- UVA 442 二十 Matrix Chain Multiplication
Matrix Chain Multiplication Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- 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 ...
- UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)
意甲冠军 由于矩阵乘法计算链表达的数量,需要的计算 后的电流等于行的矩阵的矩阵的列数 他们乘足够的人才 非法输出error 输入是严格合法的 即使仅仅有两个相乘也会用括号括起来 并且括号中 ...
- Matrix Chain Multiplication[HDU1082]
Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- UVa442 Matrix Chain Multiplication
// UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...
- 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 ...
- 【例题 6-3 UVA - 442】Matrix Chain Multiplication
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用栈来处理一下表达式就好. 因为括号是一定匹配的.所以简单很多. ab x bc会做abc次乘法. [代码] #include< ...
随机推荐
- sort() 方法用于对数组的元素进行排序
语法 arrayObject.sort(sortby) 参数 描述 sortby 可选.规定排序顺序.必须是函数. 返回值 对数组的引用.请注意,数组在原数组上进行排序,不生成副本. 说明 如果调用该 ...
- Balloon Comes!
Problem Description The contest starts now! How excited it is to see balloons floating around. You, ...
- jquery datatable[表格处理]
最近在公司实习发现一个额功能强大的表格解决方案,了解了一下,先总结如下: 1.官网:http://www.datatables.net/ 2.需要特别注意:被dataTable处理的table对象,必 ...
- Autel MaxiDAS DS708 Fatal Application Error illegal operation
I get one Original Autel MaxiDAS® DS708 Update Service, after complete update, I got a message " ...
- JVM 运行时内存结构
1.JVM内存模型 JVM运行时内存=共享内存区+线程内存区 1).共享内存区 共享内存区=持久带+堆 持久带=方法区+其他 堆=Old Space ...
- 教您如何检查oracle死锁,决解死锁
oracle死锁问题一直困扰着我们,下面就教您一个oracle死锁的检查方法,如果您之前遇到过oracle死锁方面的问题,不妨一看…… oracle死锁问题一直困扰着我们,下面就教您一个oracle死 ...
- nc命令详解
NetCat,在网络工具中有“瑞士军刀”美誉,其有Windows和Linux的版本.因为它短小精悍(1.84版本也不过25k,旧版本或缩减版甚至更小).功能实用,被设计为一个简单.可靠的网络工具,可通 ...
- [转]如何制作tizen镜像文件(图文教程)?
http://blog.csdn.net/flydream0/article/details/9163119 之前已讲解了如何下载及编译tizen源码(http://blog.csdn.net/fly ...
- AI-->从新建文档开始说起,串联相关色彩知识
相关概念:AI.PS.矢量图形 AI: Adobe Illustrator 是Adobe公司出品的一款用于矢量图形设计的软件. 矢量图形:用通俗的大白话讲与分辨率无关,可以任意的放大缩小而不会失真图 ...
- spring小例子-springMVC+mybits整合的小例子
这段时间没更博,找房去了... 吐槽一下,自如太坑了...承诺的三年不涨房租,结果今年一看北京房租都在涨也跟着涨了... 而且自如太贵了,租不起了.. 突然有点理解女生找对象要房了.. 搬家太 ...