这个题思路没有任何问题,但还是做了近三个小时,其中2个多小时调试

得到的经验有以下几点:

  1. 一定学会调试,掌握输出中间量的技巧,加强gdb调试的学习
  2. 有时候代码不对,得到的结果却是对的(之后总结以下常见错误)
  3. 能用结构体,就别用数组,容易出错(暂时还不知道为什么)=>现在知道申请的数组空间在运行期间被释放,除非用malloc去申请数组
  4. 代码要规范,空格该有就要有
  5. 有些不规范表达式,不同编译器出现不同结果,注意应避免使用这类语句

像这道题主要坑在了第三点上,以后要注意避免

以下是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的更多相关文章

  1. 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 ...

  2. UVA 442 二十 Matrix Chain Multiplication

    Matrix Chain Multiplication Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  3. 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 ...

  4. UVa 442 Matrix Chain Multiplication(矩阵链,模拟栈)

    意甲冠军  由于矩阵乘法计算链表达的数量,需要的计算  后的电流等于行的矩阵的矩阵的列数  他们乘足够的人才  非法输出error 输入是严格合法的  即使仅仅有两个相乘也会用括号括起来  并且括号中 ...

  5. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. UVa442 Matrix Chain Multiplication

    // UVa442 Matrix Chain Multiplication // 题意:输入n个矩阵的维度和一些矩阵链乘表达式,输出乘法的次数.假定A和m*n的,B是n*p的,那么AB是m*p的,乘法 ...

  7. Matrix Chain Multiplication(表达式求值用栈操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1082 Matrix Chain Multiplication Time Limit: 2000/100 ...

  8. ACM学习历程——UVA442 Matrix Chain Multiplication(栈)

    Description   Matrix Chain Multiplication  Matrix Chain Multiplication  Suppose you have to evaluate ...

  9. 【例题 6-3 UVA - 442】Matrix Chain Multiplication

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用栈来处理一下表达式就好. 因为括号是一定匹配的.所以简单很多. ab x bc会做abc次乘法. [代码] #include< ...

随机推荐

  1. google gflags使用.

    code.google.com 被墙的好开心... gflags很简单. 编译使用都很简单. (不像omaha这种丧心病狂的编译依赖). cmake 生成一下. 一路顺风顺水. 值得注意的是:  默认 ...

  2. PN-Traniger

        首先先从Bezier说起: 一条直线上有两个端点,P0和P1,那么直线可以写成 y = kx+b ,其实也就是P(t) = (1-t)P0 + P1 (这是个插值函数),(小注,我时常把这两个 ...

  3. TCP/IP协议原理与应用笔记21:路由选择的方法

    1. 路由选择的方法 (1)基本思想 不是收到IP分组后才能为其选路,而是预先获得所有的目的的路由(Routing Protocol) IP报文按预定的路由转发(route table) (2)预定路 ...

  4. 11. Android框架和工具之 Logger(调试代码)

    1. Logger Logger是android是一个简单.漂亮.功能强大的Android日志程序. 日志程序提供了 : 线程信息Thread information 类信息Class informa ...

  5. 【阿里云产品公测】离线归档OAS,再也不用担心备份空间了

    [阿里云产品公测]离线归档OAS,再也不用担心备份空间了 作者:阿里云用户莫须有3i 1 起步  1.1 初识OAS  啥是OAS,请看官方说明: 引用: 开放归档服务(Open Archive Se ...

  6. DML 数据操控语言

    一.DML数据操作语言  (DQL  select ) 主要用于检索.插入和修改数据库信息.它是最常用的SQL命令,如INSERT(插入).UPDATE(更新).SELECT(选择).DELETE(删 ...

  7. 关于MSSQL导入导出时主键与约束丢失的问题解决

    导入数据时,使用默认选项,会丢失主键.约束.默认值等属性,按如下步骤操作: -->导出向导 -->选择数据源 -->选择目的 -->指定表复制或查询:不要使用默认选项,选择“在 ...

  8. nodejs5-package.json

    name:包名,唯一,由小写字符.数字和下划线组成,不能有空格 preferglobal:是否支持全局安装,true表示支持 descrition:描述 version:版本号 author:作者信息 ...

  9. js里正则表达式详解

    详细内容请点击 正则表达式中的特殊字符 字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/ ...

  10. 破解软件感悟-PE文件格式之实例总结(五)

    有很多介绍PE文件的文章,但是我打算写一篇关于输入表的文章,因为它对于破解很有用.     我想解释它的最好的方法是举一个例子,你可以跟着我逐步深入,一步一步的思考,最后你将完全明白,我选择了一个我刚 ...