1. use fgets, and remove the potential ‘\n’ in the string’s last postion.
  2. (main point) remove redundancy

    there must be a stack, at first sight, you need a stack of type myNode, but think deeper, matrix multiplication is valid only if A.c=B.r, then the num of elementary multiplication is A.r*A.c*B.c, note that since A.c=B.r for every contiguous pair of matrices, so we can store the first matrix’s r and c, and for the rest, we first check validation, if error, break, else just store B.c, the B.r is not to been stored, thus remove redundancy.

    //
#include <cstdio>
#include <cstring>
#include <algorithm> #define MAXSIZE 1000
struct myNode{ int r,c; }; int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n,i,ch,len,iserror,res, stk[MAXSIZE];
char buf[MAXSIZE], *p;
myNode matrices[26], *mat=matrices-'A';
scanf("%d\n",&n);
if(n<=0) return -1;
for(i=0;i<n;++i) {
ch=getchar();
scanf("%d%d\n",&mat[ch].r,&mat[ch].c);
}
while(fgets(buf,MAXSIZE,stdin)) {
len=strlen(buf);
if(buf[len-1]='\n') buf[--len]=0;
for(res=0,iserror=0, p=buf+1;*p!=0 && *p=='(';++p) {}
if(*p!=0) {
stk[0]=mat[*p].r, stk[1]=mat[*p].c;
for(len=1, ++p;*p!=0;++p) {
if(*p=='(') continue;
else if(*p==')') {
--len;
res+=stk[len-1]*stk[len]*stk[len+1];
stk[len]=stk[len+1];
}
else {
if(mat[*p].r!=stk[len]) { iserror=1; break; }
stk[++len]=mat[*p].c;
}
}
while(len>1) {
--len;
res+=stk[len-1]*stk[len]*stk[len+1];
stk[len]=stk[len+1];
}
}
if(iserror) puts("error");
else printf("%d\n",res);
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.

最新文章

  1. mybatis配置文件的bug
  2. Java条件语句练习
  3. supervisor 配置
  4. [改善Java代码]建议采用的顺序是 List&lt;T&gt;、List&lt;?&gt;、List&lt;Object&gt;
  5. Oracle中字段的修改操作语法
  6. HTML5 实现图像模糊算法
  7. CPU指令的流水线运行
  8. linux kernel的函数与抽象层
  9. 【iOS发展-44】通过案例谈iOS重构:合并、格式化输出、宏观变量、使用数组来存储数据字典,而且使用plist最终的知识
  10. Ubuntu14.04 安装Oracle JDK
  11. 将vue的项目打包后通过百度的BAE发布到网上的流程
  12. 《JavaScript高级程序设计》笔记:客户端检测(九)
  13. url获取整理
  14. hadoop记录-Hadoop参数汇总
  15. 多个router和多个network
  16. [Winfrom] 使用一个启动快捷方式,打开2个不同的窗体并且共用一个缓存空间
  17. Web测试和App测试有什么区别
  18. html 之表单,div标签等。。。。。。。
  19. webstorm过期最新激活方法
  20. BEGIN-2_蓝桥杯_序列求和

热门文章

  1. JS中函数声明与函数表达式的不同
  2. 使用Spring AsyncRestTemplate对象进行异步请求调用
  3. 【树莓派】关于tinyproxy问题处理
  4. hadoop2.7.3配置文件中过时的属性
  5. 单据UI代码开发
  6. Mac下搭建android开发环境
  7. Mac 安装 MySQL
  8. RecyclerView 制作瀑布流图片
  9. .Net架构必备工具列表
  10. an interview question(2)