例题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< ...
随机推荐
- 手动实现 KVO
来源:伯乐在线 - Jerry4me 链接:http://ios.jobbole.com/88828/ 点击 → 申请加入伯乐在线专栏作者 我的Github地址 : https://github.co ...
- 一步一步实现iOS微信自动抢红包
微信红包 前言:最近笔者在研究iOS逆向工程,顺便拿微信来练手,在非越狱手机上实现了微信自动抢红包的功能. 此教程所需要的工具/文件 yololib class-dump dumpdecrypte ...
- WPF 之 窗口间传参数
有如下几种方式: 1) 声明个全局变量,就是App.xaml里面声明:在所有窗体里面都可以引用 Application.Current.Properties["ArgumentName&qu ...
- EntityFramework优缺点(转)
Entity Framework 是微软推荐出.NET平台ORM开发组件, 现在已放源代码. 以下我们来讨论一下优缺点和一些问题, 以下简称EF. 有兴趣可查询官网的Entity Framewo ...
- 亚马逊副总裁谈Marketplace平台的个性化服务
说到个性化,亚马逊无疑是挖掘与利用数据为消费者打造个性化网购体验的先驱之一.而现在,几乎所有的公司和网站都在利用更加个性化的推荐算法为用户提供更好的购物和浏览体验. 亚马逊近年来尤其重视将其个性化特性 ...
- shake震动动画
- create feature from text file
'''---------------------------------------------------------------------------------- Tool Name: Cre ...
- 巧用FileShare解决C#读写文件时文件正由另一进程使用的bug
在使用C#进行文件读写的时候,一旦对文件操作频繁,总会碰到一些令人措手不及的意外.例如经常会碰到的一个问题: System.IO.IOException: 文件“XXX”正由另一进程使用,因此该进程无 ...
- 《MFC游戏开发》笔记九 游戏中的碰撞判定初步&怪物运动简单AI
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9374935 作者:七十一雾央 新浪微博:http:// ...
- VMware系统运维(十六)部署虚拟化桌面 Horizon View Manager 5.2 配置池
1.点击"添加",打开添加池界面,选择"自动池",点击"下一步" 2.选择"专用,启动自动分配",点击"下一步 ...