HDU 1082 Matrix Chain Multiplication
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <map> 5 #include <string> 6 7 using namespace std; 8 9 struct matrix{ 10 int row,col; 11 }; 12 13 int main(){ 14 int n; 15 cin >>n; 16 map<string,matrix> a; 17 for(int i = 0;i < n; i ++ ){ 18 string name; 19 int r,c; 20 cin>>name>>r>>c; 21 matrix tmpm; 22 tmpm.row = r; 23 tmpm.col = c; 24 a.insert(pair<string,matrix>(name,tmpm)); 25 } 26 vector<string> ebnf; 27 string tmp; 28 while(cin >>tmp) ebnf.push_back(tmp); 29 30 for(int i = 0; i < ebnf.size(); i ++ ){ 31 vector<string> sstack; 32 int cnt = 0; 33 bool error = false; 34 for(int j = 0; j < ebnf[i].length(); j ++ ){ 35 if(ebnf[i][j] == '(') continue; 36 else if(ebnf[i][j] != ')') { 37 string tmp = ""; 38 tmp +=ebnf[i][j]; 39 sstack.push_back( tmp); 40 } 41 else{ 42 int idx = sstack.size()-1; 43 if( a[sstack[idx-1]].col != a[sstack[idx]].row ) { error = true; break;} 44 cnt += a[sstack[idx-1]].row*a[sstack[idx-1]].col*a[sstack[idx]].col; 45 string newName = sstack[idx-1]+sstack[idx]; 46 int newRow = a[sstack[idx-1]].row, newCol = a[sstack[idx]].col; 47 48 matrix tmpm; 49 tmpm.row = newRow; 50 tmpm.col = newCol; 51 52 a.insert( pair<string,matrix>( newName,tmpm) ); 53 sstack.pop_back();sstack.pop_back(); 54 sstack.push_back(newName); 55 } 56 } 57 if(error) cout<<"error"<<endl; 58 else cout<<cnt<<endl; 59 sstack.clear(); 60 } 61 return 0; 62 }
第一次在codeforces上能编译通过,但提交时报
error C2678: 二进制“>>”: 没有找到接受“std::istream”类型的左操作数的运算符(或没有可接受的转换)
结果是因为没有加#include <string>
当再次用codeforces编译时仍能通过,但提交时报错
a.insert(pair<string,matrix>(name,{t,c}));//不能通过
利用了个临时变量将其改为
matrix tmpm; tmpm.row = r; tmpm.col = c; a.insert(pair<string,matrix>(name,tmpm));