题目链接如下:
代码如下:
- #include
- #include
- #include
- #include
- #include
- // #define debug
-
- int n, r, c;
- char ch;
- std::string str;
- std::map<char, int> row, col;
-
- void judge(std::string &ss){
- int mul = 0;
- char m1, m2;
- char curr = 'Z' + 1;
- std::stack<char> st;
- for (int i = 0; i < ss.size(); ++i){
- if (ss[i] == ')'){
- m2 = st.top();
- st.pop();
- m1 = st.top();
- st.pop();
- st.pop();
- if (col[m1] != row[m2]){
- printf("error\n");
- return;
- }
- mul += row[m1] * col[m2] * col[m1];
- row[curr] = row[m1];
- col[curr] = col[m2];
- st.push(curr++);
- } else {
- st.push(ss[i]);
- }
- }
- printf("%d\n", mul);
- }
-
- int main(){
- #ifdef debug
- freopen("0.txt", "r", stdin);
- freopen("1.txt", "w", stdout);
- #endif
- scanf("%d\n", &n);
- while (n--){
- scanf("%c %d %d\n", &ch, &r, &c);
- row[ch] = r;
- col[ch] = c;
- }
- while (getline(std::cin, str)){
- judge(str);
- }
- #ifdef debug
- fclose(stdin);
- fclose(stdout);
- #endif
- return 0;
- }