传送门:点击打开链接
Matrix Chain Multiplication
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2606 | Accepted: 1584 |
Description
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.
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
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):
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 } Line = Expression Expression = Matrix | "(" Expression Expression ")" Matrix = "A" | "B" | "C" | ... | "X" | "Y" | "Z"
Output
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
没什么算法难度,就是有点思维难度的水题,值得一做,顺便锻炼一下思维
plus:我们可以举一反三:如果题目中没有给出括号位置,而是只给出一串矩阵,让你填入括号,使得值最小,请问怎么填? 多想想总没坏处
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cstdio>
#include<vector>
#include<climits>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int, int>P;
struct node
{
int x, y;
node(){}
node(int x, int y):x(x), y(y){}
}rec[30];
struct nd
{
node n;
ll ans;
nd(){}
nd(node n, ll ans):n(n), ans(ans){}
};
string str;
int br[1000];
map<int, int>mp;
nd dfs(int beg, int en)
{
if(beg == en) return nd(rec[str[beg]-'A'], 0LL);
if(str[beg] == '(')
{
int ano = mp[beg];
nd nd1 = dfs(beg + 1, ano - 1), ans;
if(ano + 1 > en)
{
ans = nd(node(nd1.n.x, nd1.n.y), nd1.ans);
}
else
{
nd nd2 = dfs(ano + 1, en);
ans = nd(node(nd1.n.x, nd2.n.y), nd1.ans + nd2.ans + (ll)nd1.n.x*(ll)nd2.n.x*(ll)nd2.n.y);
}
return ans;
}
else
{
int i;
node tpnode; nd tpnd, ans;
for(i = beg; i <= en; i++)
{
if(str[i] == '(')
break;
else
{
if(i == beg)
{
tpnode = node(rec[str[i]-'A'].x, rec[str[i]-'A'].y);
tpnd = nd(tpnode, 0);
}
else
{
node tmp = tpnode;
tpnode = node(tpnode.x, rec[str[i]-'A'].y);
tpnd = nd(tpnode, tpnd.ans + (ll)tmp.x * (ll)rec[str[i]-'A'].x * (ll)rec[str[i]-'A'].y);
}
}
}
if(i == en + 1)
{
ans = tpnd;
}
else
{
nd ansnd = dfs(i, en);
ans = nd(node(tpnd.n.x, ansnd.n.y), tpnd.ans + ansnd.ans + (ll)tpnd.n.x*(ll)ansnd.n.x*(ll)ansnd.n.y);
}
return ans;
}
}
int main()
{
char ch;
int n, x, y;
scanf("%d", &n);
getchar();
for(int i = 0; i < n; i++)
{
scanf("%c%d%d", &ch, &x, &y);
rec[ch-'A'] = node(x, y);
getchar();
}
while(cin >> str)
{
int len = str.size();
if(len == 1)
{
printf("0\n");
continue;
}
bool ju = true;
node tmp = node(-1, -1);
for(int i = 0; i < len; i++)
{
if(str[i] == '(' || str[i] == ')')
continue;
if(tmp.x == -1 && tmp.y == -1)
tmp = rec[str[i] - 'A'];
else
{
if(tmp.y != rec[str[i] - 'A'].x)
{
ju = false;
break;
}
else
{
tmp = node(tmp.x, rec[str[i] - 'A'].y);
}
}
}
if(!ju) printf("error\n");
else
{
memset(br, -1, sizeof(br));
mp.clear();
int cnt = 0;
for(int i = 0; i < len; i++)
{
if(str[i] == '(')
{
br[cnt++] = i;
}
else if(str[i] == ')')
{
cnt--;
mp.insert(P(br[cnt], i));
mp.insert(P(i, br[cnt]));
}
}
nd ans = dfs(0, len - 1);
printf("%lld\n", ans.ans);
}
}
}