poj 2246 (zoj 1094)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1094
ZOJ Problem Set - 1094 Matrix Chain Multiplication -------------------------------------------------------------------------------- Time Limit: 2 Seconds Memory Limit: 65536 KB -------------------------------------------------------------------------------- Matrix multiplication problem is a typical example of dynamical programming. Suppose you have to evaluate an expression like A*B*C*D*E where A,B,C,D and E are matrices. Since matrix multiplication is associative, the order in which multiplications are performed is arbitrary. However, the number of elementary multiplications needed strongly depends on the evaluation order you choose.
For example, let A be a 50*10 matrix, B a 10*20 matrix and C a 20*5 matrix.
There are two different strategies to compute A*B*C, namely (A*B)*C and A*(B*C).
The first one takes 15000 elementary multiplications, but the second one only 3500. Your job is to write a program that determines the number of elementary multiplications needed for a given evaluation strategy. Input Specification
Input consists of two parts: a list of matrices and a list of expressions.
The first line of the input file contains one integer n (1 <= n <= 26), representing the number of matrices in the first part. The next n lines each contain one capital letter, specifying the name of the matrix, and two integers, specifying the number of rows and columns of the matrix.
The second part of the input file strictly adheres to the following syntax (given in EBNF): SecondPart = Line { Line } <EOF>
Line = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z" Output Specification
For each expression found in the second part of the input file, print one line containing the word "error" if evaluation of the expression leads to an error due to non-matching matrices. Otherwise print one line containing the number of elementary multiplications needed to evaluate the expression in the way specified by the parentheses.
Sample Input
9
A 50 10
B 10 20
C 20 5
D 30 35
E 35 15
F 15 5
G 5 10
H 10 20
I 20 25
A
B
C
(AA)
(AB)
(AC)
(A(BC))
((AB)C)
(((((DE)F)G)H)I)
(D(E(F(G(HI)))))
((D(EF))((GH)I)) Sample Output
0
0
0
error
10000
error
3500
15000
40500
47500
15125
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <stack>
using namespace std;
struct node
{
int m,n;
// bool f;
};
node hash[200];
char s[1000];
int main()
{
int i,n,sum;
char c;
bool b;
scanf("%d",&n);
while(n--)
{
getchar();
scanf("%c",&c);
scanf("%d%d",&hash[c].m,&hash[c].n);
}
node temp,temp1;
while(scanf("%s",s)!=EOF)
{ sum=0;b=1;
stack<char> s1;
stack<node> s2;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='(')
s1.push(s[i]);
else if(s[i]==')')
{
c=s1.top();
s1.pop();
s1.pop();
while(!s1.empty()&&s1.top()!='(')
{
temp=s2.top();
s2.pop();
temp1=s2.top();
if(temp1.n!=temp.m)
{
b=0;break;
}
sum+=temp1.m*temp1.n*temp.n;
temp1.n=temp.n;
s2.pop();
s2.push(temp1);
s1.pop();
}
s1.push(c);
}
else
{
if(!s1.empty()&&s1.top()!='(')
{
temp=s2.top();
if(temp.n!=hash[s[i]].m)
{
b=0;break;
}
sum+=temp.m*temp.n*hash[s[i]].n;
temp.n=hash[s[i]].n;
s2.pop();
s2.push(temp);
}
else
{
s2.push(hash[s[i]]);
s1.push('#');
}
}
}
if(b)
printf("%d\n",sum);
else
printf("error\n");
}
return 0;
}
poj 2246 (zoj 1094)的更多相关文章
- POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...
- POJ 2260(ZOJ 1949) Error Correction 一个水题
Description A boolean matrix has the parity property when each row and each column has an even sum, ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- poj 3335(半平面交)
链接:http://poj.org/problem?id=3335 //大牛们常说的测模板题 ------------------------------------------------- ...
- poj 3122 (二分查找)
链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- POJ——3169Layout(差分约束)
POJ——3169Layout Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14702 Accepted ...
- POJ 3252 (数位DP)
###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...
- POJ 3368 (ST表)
链接:http://poj.org/problem?id=3368 题意:给出n个连续单调不递减数,q次询问,每次询问区间(L,R)出现频率最多的数,问出现了多少次 思路:因为n个数是单调不递减的,所 ...
随机推荐
- 从C到汇编:栈是计算机工作的基础
作者:r1ce 原创作品转载请注明出处 <Linux内核分析> MOOC课程http://mooc.study.163.com/course/U ...
- Android中的Surface和SurfaceView
一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...
- 5事件DOM零级事件跟DOM二级事件
事件的行为传播,行为本身跟事件绑定没有关系:1.全新认识事件(某一个具体的行为)->行为本身:浏览器天生自带的一些行为操作->click,mouseover(mouseenter),mou ...
- jsp中的c标签
核心标签库 引用: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> ...
- project facet java version 1.6 is not supported
可能你用的jdk1.5的包,而开发是用的jdk1.6,不允许1.5进行安装 法1,选中项目 Properties , 选择 Project Facets,右击选择 Java , Change Vers ...
- android-'Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 8'
解决的方案是将jdk1.7制定的版本定制为jdk.6.即 在eclipse中,右键项目->Properties->Java Compiler->enable "projec ...
- C# Double类型 不四舍五入
测试中发现Double类型需要#0.00 小数点精度为后2位,并且多余部分不需要四舍五入,直接截断 用字符串处理也可以,但是比较麻烦 这里给出一种思路: double a = 9999.999; a ...
- Cacti添加threshold、monitor和setting
Cacti版本:Version 0.8.8b 一.插件介绍: monitor:通过简单明了的图标提供服务器的运行状态 settings:给不同的插件提供一些共用的信息,如邮件信息,dns信息thold ...
- 关于T-SQL重编译那点事,内联函数和表值函数在编译生成执行计划的区别
本文出处:http://www.cnblogs.com/wy123/p/6266724.html 最近在学习 WITH RECOMPILE和OPTION(RECOMPILE)在重编译上的区别的时候,无 ...
- 用 javascript 判断 IE 版本号
原文地址: http://julying.com/blog/determine-the-version-number-of-ie-with-javascript/ var _IE = (functio ...