UVA442栈(2)代码中为什么结构体可以那么写?)


1)原题代码1

#include<cstdio>
#include<stack>
#include<iostream>
#include<string>

using namespace std;
const int maxn=103;
struct sta{
    int a;
    int b;
    sta(int a=0,int b=0):a(a),b(b) {}
}z[maxn];
stack<sta>s;//可以将结构体直接压入栈,而不是将结构体中某个变量作为标志入栈
int main()
{
    int num;
    cin>>num;
    for(int i=0;i<num;i++){
        string name;
        cin>>name;
        int name_num=name[0]-'A';//直接对应序号就不用为新合成的矩阵起名而烦恼,序号依次递增即可
        cin>>z[name_num].a>>z[name_num].b;
    }
    string exp;//用string而不用getchar()可以省去很多麻烦

    while(cin>>exp){
        int g=26;
        int sum=0;int bj=1;
        int len=exp.length();
        for(int i=0;i<len;i++){
            if(isalpha(exp[i])){//遇到字母入栈,或者新和成的结构体也直接入栈
                s.push(z[exp[i]-'A']);
            }
            else if(exp[i]==')'){//遇到')'出栈,遇到'('不用管
                sta z2=s.top();s.pop();
                sta z1=s.top();s.pop();
                if(z1.b!=z2.a){
                    bj=0;
                    break;
                }
                z[g].a=z1.a;z[g].b=z2.b;//z[g++].a=z1.a;z[g++].b=z2.b;之前两次g++当然要错了!!
                s.push(z[g]);
                g++;
                sta ztemp=s.top();
                sum+=(z1.a*z1.b*z2.b);
            }
        }
        if(bj)
            cout<<sum<<endl;
        else
            cout<<"error"<<endl;
        //memset(z,)
    }
    return 0;
}
2)原题代码2(为什么结构体里会这么写啊???)

#include<cstdio>
#include<stack>
#include<iostream>
#include<string>
using namespace std;

struct sta {
  int a, b;
  sta(int a=0, int b=0):a(a),b(b) {}//这是什么?111
} z[26];

stack<sta> s;

int main() {
  int num;
  cin >> num;
  for(int i = 0; i < num; i++) {
    string name;
    cin >> name;
    int name_num = name[0] - 'A';
    cin >> z[name_num].a >> z[name_num].b;
  }
  string exp;
  while(cin >> exp) {
    int len = exp.length();
    int bj = 1;
    int sum = 0;
    for(int i = 0; i < len; i++) {
      if(isalpha(exp[i])) s.push(z[exp[i] - 'A']);
      else if(exp[i] == ')') {
        sta z2 = s.top(); s.pop();
        sta z1 = s.top(); s.pop();
        if(z1.b != z2.a) { bj=0; break; }
        s.push(sta(z1.a, z2.b));<span style="font-family: Arial, Helvetica, sans-serif;">//这是什么?222</span>
        sum += (z1.a * z1.b * z2.b);

      }
    }
    if(bj)
        cout<<sum<<endl;
    else
        cout<<"error"<<endl;
  }

  return 0;
}

3)原题

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.

Input Specification

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 (  ), 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 } <EOF>
Line       = Expression <CR>
Expression = Matrix | "(" Expression Expression ")"
Matrix     = "A" | "B" | "C" | ... | "X" | "Y" | "Z"

Output Specification

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

你可能感兴趣的:(栈,STL,结构体,uva)