hdu-1082Matrix Chain Multiplication

本文介绍了一种利用栈数据结构优化矩阵链乘法运算次数的方法。通过解析输入表达式并运用栈来跟踪矩阵运算顺序,文章提供了一个有效的算法实现,以最小化矩阵相乘过程中的总乘法操作数。

栈的简单使用

Matrix Chain Multiplication

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 930    Accepted Submission(s): 634


Problem Description
Matrix multiplication problem is a typical example of dynamical programming. 

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
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 (1 <= n <= 26), 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
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
 

题目大意:这是一个关于矩阵的运算的题。要求表达式最后所有的矩阵运算中乘法的运算次数的总和。

解题思路:用栈,把表达式压入栈中,然后遇见“)”便弹出两个矩阵进行计算,并求出乘法运算的次数。(第一次亲自动手切掉一个关于栈的题目,而且还是1A,信心顿时提升十个百分点大笑)c++的STL库还是挺好用的.......

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<iostream>
#include<algorithm>
#define maxn 1100
#define LL __int64
using namespace std;

struct Matrix//用结构体存放矩阵的信息
{
    char c;
    LL row,col;
}m[30];

stack<Matrix> s;
char str[maxn];
int main()
{
    int t,i,j;
    LL sum;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
        getchar();
        scanf("%c%I64d%I64d",&m[i].c,&m[i].row,&m[i].col);
    }
    getchar();
    while(gets(str)!=NULL)
    {
        sum=0;
        int len=strlen(str);
        int flag=0;
        for(i=0;i<len;i++)
        {
            if(str[i]!='('&&str[i]!=')')//对输入的矩阵进行判断
            {
                for(j=0;j<t;j++)
                {
                    if(str[i]==m[j].c)//如果表达式中的矩阵是已知信息的矩阵,就把它压入栈中
                    {
                        s.push(m[j]);
                        break;
                    }
                }
                if(j==t)//说明表达式中的矩阵是不可知的
                    flag=1;
            }
            if(str[i]==')')//遇见一个右括号就弹出两个矩阵进行运算
            {
                Matrix temp1,temp2,temp;
                temp1=s.top();
                s.pop();
                temp2=s.top();
                s.pop();
                if(temp1.row==temp2.col)
                {
                    temp.row=temp2.row;
                    temp.col=temp1.col;
                    sum+=temp.row*temp.col*temp1.row;//求出这次运算中进行的乘法运算的次数
                    s.push(temp);//两个矩阵运算完之后会得到一个新的矩阵,保存它的信息,并且压入栈中
                }
                else flag=2;//说明这两个矩阵不能进行运算
            }
            if(flag)
                break;
        }
        if(flag)
            printf("error\n");
        else
            printf("%I64d\n",sum);
    }
    return 0;
}



Nano-ESG数据资源库的构建基于2023年初至2024年秋季期间采集的逾84万条新闻文本,从中系统提炼出企业环境、社会及治理维度的信息。其构建流程首先依据特定术语在德语与英语新闻平台上检索,初步锁定与德国DAX 40成分股企业相关联的报道。随后借助嵌入技术对文本段落执行去重操作,以降低内容冗余。继而采用GLiNER这一跨语言零样本实体识别系统,排除与目标企业无关的文档。在此基础上,通过GPT-3.5与GPT-4o等大规模语言模型对文本进行双重筛选:一方面判定其与ESG议题的相关性,另一方面生成简明的内容概要。最终环节由GPT-4o模型完成,它对每篇文献进行ESG情感倾向(正面、中性或负面)的判定,并标注所涉及的ESG具体维度,从而形成具备时序特征的ESG情感与维度标注数据集。 该数据集适用于多类企业可持续性研究,例如ESG情感趋势分析、ESG维度细分类别研究,以及企业可持续性事件的时序演变追踪。研究者可利用数据集内提供的新闻摘要、情感标签与维度分类,深入考察企业在不同时期的环境、社会及治理表现。此外,借助Bertopic等主题建模方法,能够从数据中识别出与企业相关的核心ESG议题,并观察这些议题随时间的演进轨迹。该资源以其开放获取特性与连续的时间覆盖,为探究企业可持续性表现的动态变化提供了系统化的数据基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值