hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏
- use fgets, and remove the potential ‘\n’ in the string’s last postion.
- (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) 收藏的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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) ...
- 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 ...
- 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 ...
随机推荐
- 个推,手机推送API的使用
参考:http://www.cnblogs.com/may-25/p/5501804.html 推送成功后返回的任务Id: String contentId = push.getContentId(m ...
- Qt qmake 使用(含遗留问题)
网上在介绍编译Qt的教程中,大多数都是这样提到编译的: 1, 运行 VS2012 x86 Native Tools Command Prompt 批处理 2, cd <install qt5.0 ...
- 浅谈 原生javaScript&&react 实现全局触摸按钮(附带对addeventlistener的了解)
1.采用原生javaACript 实现全局触摸按钮 首先在控制台输出,观察事件有哪些关于触摸的字段可以使用,然后拿这些字段的数据开始来写方法. 因为要做的是全局触摸按钮,我需要拿到的是按钮时时的坐标位 ...
- [SoapUI] 在SoapUI里用Java语言判断Excel单元格为空或者Null时出错
我取Excel数据时先判断cell是否为"": if(cellValue != ""){ listNumber.add(i); cellValu ...
- stm32串口输出丢失第一个字符的问题及原因
因为TC和TXE 标志位在复位的时候会被置1,导致第一次没有发送. 所以在初始化串口的时候可以增加一句 USARTx->SR=0;(如 USART1->SR=0;)
- Perl--学习记录(实时更新)
标量变量(varibale)以美元符号($)开头,这个符号也成为魔符(sigil).Perl通过魔符来区分它是什么类型的变量. Perl里面大部分变量名称习惯使用全小写.而使用全大写的(比如$ARGV ...
- SSH网上答题系统质量属性
我要做的事网上答题系统,通过注册登录到答题页面. 这其中数据库的连接靠Hibernate,数据库的增删改查用Sruts2实现. 关于Struts2的学习,仅仅在action的表面上,可以在action ...
- js 在页面上模拟多选,蚂蚁线线框
<html> <head></head> <style> body{padding-top:50px;padding-left:100px;paddin ...
- mongodb语法备份(转)
mongodb语法 MongoDB的好处挺多的,比如多列索引,查询时可以用一些统计函数,支持多条件查询,但是目前多表查询是不支持的,可以想办法通过数据冗余来解决多表查询的问题. 查询colls所有数据 ...
- ACCESS中计算日均值
如图所示,现有时间数据的时间字段是精确到时分秒的,现在需要计算PM2.5的日平均值,因此在查询时需要过滤时间字段的格式,去掉时分秒部分,只提取年月日部分. 查找资料,发现一般用CONVERT()函数实 ...