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 ...
随机推荐
- Example For maven-compiler-plugin
1. Compiling Sources Using A Different JDK The compilerVersion parameter can be used to specify the ...
- 动态加载框架DL分析
动态加载框架DL分析 插件化开发,主要解决三个问题1.动态加载未安装的apk,dex,jar等文件2.activity生命周期的问题,还有service3.Android的资源调用的问题 简单说一下怎 ...
- Ajax聊天
结构: index.html <!DOCTYPE html> <html> <head> <title>index.html</title> ...
- python爬取返利网中值得买中的数据
先使用以前的方法将返利网的数据爬取下来,scrapy框架还不熟练,明日再战scrapy 查找目标数据使用的是beautifulsoup模块. 1.观察网页,寻找规律 打开值得买这块内容 1>分析 ...
- Android应用中MVP开发模式
所谓MVP(Model-View-Presenter)模式.是将APP的结构分为三层: view - UI显示层 view 层主要负责: 提供UI交互 在presenter的控制下修改UI. 将业务事 ...
- JavaScript查找数组是否存在指定元素
利用JavaScript的函数有两种方式 1.jQuery jQuery.inArray( value, array [, fromIndex ] ) value 类型: Anything 要查找的值 ...
- 【翻译】configuration changes与handler.post
原文地址 http://corner.squareup.com/2013/12/android-main-thread-2.html 在前一部分里面previous part ,我们深入挖掘了 loo ...
- VPS/云主机 如何试用远程连接登录主机服务器_
1.windows主机如何远程登录 点本地电脑开始>运行(或者按"window+R")>输入mstsc点确定 弹出远程连接的框输入IP连接, 如果是VPS,直接输入I ...
- mysql 安装以及运行
目录: http://www.fenby.com/courses/mysqlke-cheng-lian-zai/ 1.下载 2.配置 3.启动服务器 4.启用客户端并修改用户信息 1.mysql的下载 ...
- guava学习--Objects
转载:https://my.oschina.net/realfighter/blog/349821 Java中的Object类是所有Java类的超类(也就是祖先),所有对象都实现Object类中的方法 ...