POJ 2246 Matrix Chain Multiplication

Matrix Chain Multiplication

Time Limit: 1000MS

Memory Limit: 65536K

Description

Suppose you have to evaluate an expression like ABCDE 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 5010 matrix, B a 1020 matrix and C a 205 matrix. There are
two different strategies to compute A
BC, namely (AB)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): 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

思路:

类似于判断括号序列合法的那种题。
可以用递归的方式写,也可以借助栈的存储来写。

代码(栈):

#include
#include
#include
#include
#include

using namespace std;

struct node
{
	int row;
	int column;
}a[1000];
stack<char> p;
char str[5000];
int n, cnt = 1;
int ans = 0;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int i, j;
	int ro, col, len;
	char ch, ch2;
	bool flag;
	cin >> n;
	for (i = 1; i <= n; ++i)
	{
		cin >> ch >> ro >> col;
		a[ch - 'A'].row = ro;
		a[ch - 'A'].column = col;
	}
	while (cin >> str)
	{
		flag = true;
		while (!p.empty())p.pop();
		ans = 0; cnt = 1;
		len = strlen(str);
		for (i = 0; i < len; ++i)
		{
			if (str[i] == ')')
			{
				ch2 = p.top(); p.pop();
				ch = p.top(); p.pop();
				p.pop();
				if (a[ch - 'A'].column != a[ch2 - 'A'].row) {
					flag = false;
				}
				else {
					ans = ans + a[ch - 'A'].row * a[ch - 'A'].column * a[ch2 - 'A'].column;
				}
				a[25 + cnt].row = a[ch - 'A'].row;
				a[25 + cnt].column = a[ch2 - 'A'].column;
				p.push('Z' + cnt);
				cnt++;
			}
			else
				p.push(str[i]);
		}
		if (flag)
			cout << ans << endl;
		else
			cout << "error" << endl;
	}
	return 0;
}

代码(递归)

#include

using namespace std;

struct triple
{
	int row;
	int column;
	int mul;
};
char e[500];
int p = 0;
int row[100], col[100];
bool flag = true;

triple expression()
{
	triple t;
	triple t1, t2;
	if (e[p] == '(')
	{
		p++;
		t1 = expression();
		t2 = expression();
		p++;
		if (t1.column != t2.row)
			flag = false;
		t.row = t1.row;
		t.column = t2.column;
		t.mul = t1.mul + t2.mul + t1.row * t1.column * t2.column;
	}
	else
	{
		t.row = row[e[p]];
		t.column = col[e[p]];
		t.mul = 0;
		p++;
	}
	return t;
}

int main()
{
	int n, i, j;
	int ro, co;
	triple tmp;
	char ch;
	cin >> n;
	for (i = 0; i < n; ++i){
		cin >> ch >> ro >> co;
		row[ch] = ro; col[ch] = co;
	}
	while (cin >> e)
	{
		p = 0; flag = true;
		tmp = expression();
		if (flag)
			cout << tmp.mul << endl;
		else
			cout << "error" << endl;
	}
	return 0;
}

你可能感兴趣的:(递归回溯)