-
//zoj1094 Matrix Chain Multiplication
-
//Accepted 1094 C ++ 00:00.01 848K
-
#include <cstdio>
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
struct matrix{ int row,col;}m[26],t[26];
-
int sum;
-
int index(char c){ return c-'A';}
-
void backtrack(string s)
-
{
-
int i,j,k;
-
if (s.length()==4){
-
i = index(s[1]), j = index(s[2]);
-
if (m[i].col==m[j].row){
-
sum += m[i].row*m[j].row*m[j].col;
-
cout << sum << endl;
-
}else cout << "error" << endl;
-
return;
-
}
-
for (k=1; s[k]!=')'; ++k);
-
i = index(s[k-2]), j = index(s[k-1]);
-
if (m[i].col==m[j].row){
-
sum += m[i].row*m[j].row*m[j].col;
-
m[i].col = m[j].col;
-
s.erase(k-1,2);
-
s.erase(k-3,1);
-
backtrack(s);
-
}else cout << "error" << endl;
-
}
-
int main()
-
{
-
#ifdef ONLINE_JUDGE
-
#else
-
freopen("1094.txt","r",stdin);
-
#endif
-
int i,n;
-
cin>>n;
-
char c;
-
for (i=0; i<n; ++i)
-
cin >> c >> t[i].row >> t[i].col;
-
string s;
-
while (cin >> s){
-
if (s.length()==1){
-
cout << 0 << endl;
-
continue;
-
}
-
for (i=0; i<n; ++i) m[i].row = t[i].row, m[i].col = t[i].col;
-
sum = 0;
-
backtrack(s);
-
}
-
#ifdef ONLINE_JUDGE
-
#else
-
fclose(stdin);
-
#endif
-
return 0;
-
}
zoj1094 Matrix Chain Multiplication
最新推荐文章于 2019-09-26 19:42:29 发布