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. C#知识点总结系列:C# 数据结构

    线性表(Linear List) 线性表是一个线性结构,它是一个含有n≥0个结点的有限序列,对于其中的结点,有且仅有一个开始结点没有前驱但有一个后继结点,有且仅有一个终端结点没有后继但有一个前驱结点, ...

  2. OpenCV从入门到放弃系列之——图像的基本操作

    读取.修改.保存图像 图像读取函数imread(); 图像颜色空间的转换cvtColor(); 图像保存至硬盘imwrite(); /********************************* ...

  3. 16-阿里-intership

  4. understanding ECMAscript 6 ---- block bindings

    Traditionally, the way variable declarations work has been one tricky part of programming in javascr ...

  5. JQuery_DOM 简介/设置元素及内容

    一.DOM 简介 1.D 表示的是页面文档Document.O 表示对象,即一组含有独立特性的数据集合.M表示模型,即页面上的元素节点和文本节点. 2.DOM 有三种形式,标准DOM.HTML DOM ...

  6. 学习_单片机/嵌入式_的资源链接。——Arvin

    ---恢复内容开始--- +单片机 -郭天祥的51单片机入门视频(链接: https://pan.baidu.com/s/1sl3xNDr 密码: 1zik) -51单片机20元购买资料公开(链接: ...

  7. DBUnit的一些注意事项

    DatabaseOperation.TRUNCATE_TABLE.execute(con, ds);等对数据库的操作放在每个@Test方法中做而不是在@Before里做,可增加灵活性

  8. java selenium针对多种情况的多窗口切换

    一.通过按钮点击打开的新页面,不涉及到打开多窗口,只要在已有打开的窗口实现切换操作即可 Set<String> winHandels = driver.getWindowHandles() ...

  9. mybatis 对于基本类型数据传值的问题

    最近在开发的时候,遇到一个小问题: Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter fo ...

  10. Mac终端下打开sublime

    我安装的sublime 2终端输入如下命令 alias subl=\''/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl ...