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)的更多相关文章

  1. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  2. POJ 2260(ZOJ 1949) Error Correction 一个水题

    Description A boolean matrix has the parity property when each row and each column has an even sum, ...

  3. POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配

    两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...

  4. poj 3335(半平面交)

    链接:http://poj.org/problem?id=3335     //大牛们常说的测模板题 ------------------------------------------------- ...

  5. poj 3122 (二分查找)

    链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1 ...

  6. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  7. POJ——3169Layout(差分约束)

    POJ——3169Layout Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14702   Accepted ...

  8. POJ 3252 (数位DP)

    ###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...

  9. POJ 3368 (ST表)

    链接:http://poj.org/problem?id=3368 题意:给出n个连续单调不递减数,q次询问,每次询问区间(L,R)出现频率最多的数,问出现了多少次 思路:因为n个数是单调不递减的,所 ...

随机推荐

  1. git的一些概念和技巧

    1. 分支代表最后三个commit(即HEAD, HEAD^和HEAD~2),前一个commit,也用HEAD~1 2. 查看一个文件的改动历史git log (--pretty=oneline) - ...

  2. Android中强大的Matrix操作

    简介: Matrix翻译字面意思时矩阵,在Android的API中提供了两种Matrix,分别是android.graphics.Matrix 和 android.opengl.Matrix . 后者 ...

  3. JSON 遍历转为Model Bean

    @RequestMapping(value = "/batchAddPageIndexBrand") @ResponseBody public HashMap<String, ...

  4. 如何做高大上的网站布局 -------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )

    SEO开始做,最重要的是网站布局,一个网站布局决定了用户在网站的停留时间,在网站中放入用户想要的内容之外,更重要的是要让用户看到网站之后,一目了然,视觉和感官上良好的体验.那新手该如何做网站布局呢? ...

  5. ASP.NET5配置

    ASP.NET5支持各种各样的配置,应用程序配置数据可以来自JSON, XML或者INI格式的文件,也能来自环境变量,你也可以自定义你自己的Configuration Provider. 1. 获取和 ...

  6. HTML5 File 对象

    实例说明1: <div class="container"> <input type="file" id="file" m ...

  7. 织梦dede自定义内容分页,datalist运用实例

    在/plus文件夹中新建一个ceshi.php文件..<?php require(dirname(__FILE__)."/../include/common.inc.php" ...

  8. Landsat元数据批量下载工具

    目录 前言 landsat数据情况简介 下载元数据 总结 一.前言        最近由于工作需要,需要下载部分landsat数据的元数据,老板大手一挥,给了十几年的landsat的path.row以 ...

  9. 读书笔记 |Google C++编程风格指南

    Google C++编程风格指南 ## 0. 背景 每一个C++程序员都知道,C++具有很多强大的语言特性,但这种强大不可避免的导致它的复杂,这种复杂会使得代码更易于出现bug.难于阅读和维护. 本指 ...

  10. Reverse Interger

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Have you ...