例题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< ...
随机推荐
- 小白日记51:kali渗透测试之Web渗透-WebShell(中国菜刀、WeBaCoo、Weevely)
webshell 本质:<?php echo shell_exec($_GET['cmd']);?> windows平台 中国菜刀官网:胖客户端程序,国产中比较优秀的webshell,适用 ...
- 开发工具 之 PowerDesigner 应用积累
1.在默认情况下,code与name是联动,修改了name中的数据. 解决方法:设置菜单栏选择"Tools→General Options→Dialog" 中的 "Na ...
- sql语句,一个全角空格的考验
早晨在群里灌水.突然有人发了这个,问哪里错了,下图是sql语句和报错信息... 一群人猜了半天,呵呵,最后发现是 ”全角空格“ 引起的...真是醉了..记录下,引以为戒.
- 【¥200代金券、iPad等您来拿】 阿里云9大产品免费公测#10月9日-11月6日#
#10.09-11.06#200元代金券.iPad大奖, 9大产品评测活动! 亲爱的阿里云小伙伴们: 云产品的多样性(更多的云产品)也是让用户深度使用云计算的关键.今年阿里云产品线越来越丰富,小云搜罗 ...
- 【阿里云产品评测】小站长眼中的巅峰云PK
[阿里云产品评测]小站长眼中的巅峰云PK 阿里云论坛用户:昵称-a5lianmeng 笔者是一名小站长,因狂热互联网,而在毕业后由宅男逐渐进入站长队伍,在毕业后的几年间,经营6个流量类网站,身为站长, ...
- Linux下安装和设置memcache(转)
memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...
- 你有没有试过“闭上眼”使用:京东、滴滴、QQ、支付宝?
正在看这篇文章的同学,也许是幸运的. 互联网的发展,让我们的生活越来越便利,但这个“我们”,也许并不包括那些残障人士.正常人眼里来说再简单不过的页面操作,对于盲人来说都是不可攀越的高墙.换句话说,越行 ...
- 如何使用虚拟机在U盘上安装linux
如何使用虚拟机在U盘上安装linux 将linux安装到U盘的方法有很多,我觉得用虚拟机还是很方便的,直接上干货 创建虚拟机 我用的vbox,vmware也一样.配置随意一点就好,配置高安装的也快. ...
- CF 118E Bertown roads 桥
118E Bertown roads 题目:把无向图指定边的方向,使得原图变成有向图,问能否任意两点之间互达 分析:显然如果没有桥的话,存在满足题意的方案.输出答案时任意从一个点出发遍历一遍即可. 求 ...
- 第一个过滤器Filter
过滤器实现Filter接口javax.servlet.Filter package com.henau.example; import java.io.IOException; import java ...