Uva 442 Matrix Chain Multiplication

点击打开链接

题目意思:给定一序列的矩阵,然后对于每一个表达式求出矩阵的乘法次数

解题思路:我们可以想到利用栈的思想,如果读入的字符不是 ')' 那么我们都把它如栈,如果遇到')', 我们往回找到第一个不是左括号后面的所有矩阵然后相乘,在把结果如栈即可

注意事项:由于后面的乘法会改变值,所以我们必须开两个结构体,一个用来存放初始值,另外一个是作为计算是所用,这样就不会有错,还有这一题数据很水,如果直接考虑括号内只有两个矩阵也可以过

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
int n , sum;
char ch[100];
//结构体用来存储行数和列数
struct Matrix{
    int row;
    int col;
};

Matrix s[200];//存储输入的值
Matrix cs[200];//如果不开两个会有数据出错
stack<char>st;

//复制到另外一个结构体里面
void copy(){
    for(int i = 1 ;i <= n ; i++)
        cs[i] = s[i];
}

void solve(){
    int i = 0 , mark = 0;
    sum = 0;
    copy();
    while(ch[i]){
        //如果不是有括号就入栈
        if(ch[i] != ')')
            st.push(ch[i]);
        else{
            //建立一个临时的栈用来存储要计算的那一部分
            stack<char>temp;
            while(!temp.empty())
                temp.pop();
            //第一个左括号之前全部入栈
            while(st.top() != '('){
                temp.push(st.top());
                st.pop();
            }
            st.pop();//同时删除一个左括号
            //tempchar 表示第一个矩阵
            char tempchar;
            if(!temp.empty())
                tempchar = temp.top();
            temp.pop();
            //计算矩阵的乘法次数
            while(!temp.empty()){
                //如果两个矩阵不满足乘法就输出error
                if(cs[tempchar-64].col != cs[temp.top()-64].row){
                     printf("error\n");
                     mark = 1;
                     break;
                }
                else{
                    //这里都要对cs结构体操作,因为乘法后会改变原来的值
                    sum += cs[tempchar-64].row * cs[tempchar-64].col * cs[temp.top()-64].col;
                    cs[tempchar-64].col = cs[temp.top()-64].col;
                    temp.pop();
                }
            }
            if(mark)
                break;
            st.push(tempchar);//最后一个矩阵从新入栈
        }
        i++;
    }
    if(!mark)
        cout<<sum<<endl;
    //栈的清空
    while(!st.empty())
        st.pop();
}

//main函数实现
int main(){
    int  i;
    char c;
    cin>>n;
    getchar();
    for(i = 1 ; i <= n ; i++){
        cin>>c>>s[i].row>>s[i].col;
    }
    getchar();
    while(gets(ch)){
        //只有一个矩阵就直接输出error
        if(strlen(ch) == 1)
            cout<<0<<endl;
        else
            solve();
    }
    return 0;
}
时间: 2024-09-22 01:22:14

Uva 442 Matrix Chain Multiplication的相关文章

UVA 442:Matrix Chain Multiplication 数据结构专题

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=383 题目类型: 数据结构, 链表 样例输入: 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)) (

UVa 348 Optimal Array Multiplication Sequence:区间DP&amp;amp;矩阵链乘,MCM

348 - Optimal Array Multiplication Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=284 记忆化搜索:dp[a][b] = max(dp[a][b], dp[a][i] + dp[i + 1][b] + x

算法题:UVA 348 Optimal Array Multiplication Sequence(区间dp)

Optimal Array Multiplication Sequence Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication: The number of columns in the A array must be the same as the number of rows in the B array. Not

uva 10895 Matrix Transpose

点击打开链接uva 10895 思路: STL的vector模拟 分析: 1 看懂题目之后,直接利用两个vector模拟即可 代码: #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int MAXN = 10010; vector<int>v1[MA

ZOJ 简单题集合(二)

对以下简单题,我同时给出一个我主观认为的难度值(0.1~1.0之间). (1). ZOJ 1072: Microprocessor Simulation. (Difficulty: 0.2) http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1072 微处理器模拟,它含有两个累加器,代码和内存统一寻址,即冯诺依曼结构,比较简单. ZOJ1072_cpp #include <stdio.h>#include <stri

Mining of Massive Datasets – Link Analysis

5.1 PageRank 5.1.1 Early Search Engines and Term Spam As people began to use search engines to find their way around the Web, unethical people saw the opportunity to fool search engines into leading people to their page. Techniques for fooling search

Strassen’s algorithm for matrix multiplication

Strassen算法能够在时间内完成矩阵乘法. package chapter4; import Utils.P; class Matrix { private int[][] data; int lengthX; int lengthY; private int xs; private int ys; public Matrix(int[][] data) { this.data = data; lengthX = data.length; lengthY = data[0].length;

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic

UVa 10720:Graph Construction

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1661 原题: Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are diffe