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.

hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏的更多相关文章

  1. hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++ 分类: hdoj 2015-07-12 03:24 87人阅读 评论(0) 收藏

    the algorithm of three version below is essentially the same, namely, Kadane's algorithm, which is o ...

  2. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  3. Hiking 分类: 比赛 HDU 函数 2015-08-09 21:24 3人阅读 评论(0) 收藏

    Hiking Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  4. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

  6. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  7. HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. A simple problem 分类: 哈希 HDU 2015-08-06 08:06 1人阅读 评论(0) 收藏

    A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

随机推荐

  1. MVC开发模式之Servlet+jsp+javaBean

    Servlet+jsp+JavaBean组合开发是一种MVC开发模式,控制器Controller采用Servlet.模型Model采用JavaBean.视图View采用JSP. 1.Web开发的请求- ...

  2. 微信https请求工具类

    工作中用到的微信https请求工具类. package com.gxgrh.wechat.tools; import com.gxgrh.wechat.wechatapi.service.System ...

  3. 浅谈html5及其新特性

    什么是 HTML5? HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标准. HTML 的上一个版本诞生于 1999 年.自从那以后,Web 世界已经经历了巨变. HTML5 仍 ...

  4. activity通过onActivityResult间数据交互

    首先要创建2个activity 分别为MainActivity和OneActiivity MainActivity代码如下: package com.tp.soft.app; import andro ...

  5. MyBatis 入门(一)

    MyBatis mybatis和hibernate都属于orm(对象与关系映射) 框架 mybatis的优点: 1.sql-mapping :操作更自由,可控性高,执行效率更高 2.轻量,学习更容易 ...

  6. Android TabHost使用

    TabHost是Android中自带的选项卡控件,效果图如下: 主布局文件 <RelativeLayout xmlns:android="http://schemas.android. ...

  7. OpenCV计算点到直线的距离 数学法

    我们在检测图像的边缘图时,有时需要检测出直线目标,hough变换检测出直线后怎么能更进一步的缩小区域呢?其中,可以根据距离来再做一判断,就涉及到了点与直线的距离问题. 点到直线距离代码如下: //== ...

  8. Android 动画分类

    一:Tween Animation 动画类型 下面先来看看Android提供的动画类型.Android的animation由四种类型组成 在XML文件中: alpha        渐变透明度动画效果 ...

  9. 个人对JQuery Proxy()函数的理解

    转载至:http://www.cnblogs.com/acles/archive/2012/11/20/2779282.html JQuery.proxy(function,context): 使用c ...

  10. ubuntu的一些常用命令,测试版本:Ubuntu 12.04.5 LTS

    最近配置了一台Linux服务器,选用的是Ubuntu 12.04.5 LTS版本. 把之前放在Windows Server 2003上的网站移到了现在的服务器上,给我的感受用一个字形容:真JB快! 网 ...