Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

从风格到思路,希望大牛指点下

Posted by bigbusyboy at 2010-01-18 04:18:47 on Problem 2246
#include <iostream>
#include <string>
#include <stack>

using namespace std;

typedef struct matrix_t
{
    char name;
    int row;
    int col;
}matrix;

int N;
matrix array[30];

// init
void init();
// get index by name
int index(char name);

int main()
{
    //freopen("input.dat", "r", stdin);
    init();
    string s;
    while(cin >> s)
    {
        stack<matrix> st;
        int ans = 0;
        bool error = false;
        for(string::iterator it = s.begin(); it != s.end(); it++)
        {
            if(*it == '(') continue;
            if(*it == ')')
            {
                matrix m1 = st.top();
                st.pop();
                matrix m2 = st.top();
                st.pop();
                if(m2.col != m1.row)
                {
                    cout << "error" << endl;
                    error = true;
                    break;
                }
                matrix m;
                m.name = 0;
                m.row = m2.row;
                m.col = m1.col;
                st.push(m);
                ans += m2.row * m2.col * m1.col;
            } else{
                int i = index(*it);
                if(i == -1) break;
                st.push(array[i]);
            }
        }
        if(!error) cout << ans << endl;
    }

    return 0;
}

void init()
{
    cin >> N;
    for(int i = 0; i < N; i++)
        cin >> array[i].name >> array[i].row >> array[i].col;
}

int index(char matrix_name)
{
    for(int i = 0; i < N; i++)
        if(array[i].name == matrix_name) return i;

    return -1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator