当前位置:网站首页>[exercise-3] (UVA 442) matrix chain multiplication
[exercise-3] (UVA 442) matrix chain multiplication
2022-07-06 15:56:00 【Flame car】
Translate directly :
Input n Dimensions of matrices and expressions of matrix chain multiplication , Output the number of multiplications . If multiplication doesn't work , The output error. Jiading A yes mn Matrix ,B yes np Matrix , that AB yes mp Matrix , The number of multiplications is mn*p. If A The number of columns is not equal to B The number of rows , Then multiplication cannot be performed .
for example ,A yes 50 * 10 Of ,B yes 10 * 20 Of ,C yes 20 * 5 Of , be (A(BC)) Is multiplied by 10 * 20 * 5(BC The number of multiplications ) + 50 *10 * 5((A(BC)) The number of multiplications ) = 3500.
AC Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
const ll mod = 1e9+7;
struct node{
int x;
int y;
}a[26];
int main()
{
int n;
char ch;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>ch;
cin>>a[ch-'A'].x;
cin>>a[ch-'A'].y;
}
string s;
while(cin>>s)
{
stack<node> st;
int f=1,ans=0;
int len = s.size();
if(len == 0 )
{
cout<<0<<endl;
continue;
}
for(int i=0;i<len;i++)
{
if(s[i]>='A' && s[i]<='Z')
st.push(a[s[i]-'A']);
else if(s[i]==')')
{
node x2 = st.top();st.pop();
node x1 = st.top();st.pop();
if(x1.y != x2.x){
f = 0;
break;
}
ans+=x1.x*x1.y*x2.y;
node tmp;
tmp.x=x1.x,tmp.y=x2.y;
st.push(tmp);
}
}
if(f)
cout<<ans<<endl;
else
cout<<"error"<<endl;
}
}
Ideas :
① Use a structure to store two values of each letter .
② Stack the structure
③ encounter ‘(’ Ignore , encounter ’)' Operate once when .
④ Content of operation : Take out the first two elements of the stack for matrix chain multiplication ( Note that the first one is the back one ,AB Perform matrix chain multiplication , Stack pops up first B Pop up after A), Put the newly obtained element on the stack .
⑤ If matrix chain multiplication cannot be performed, mark and exit .
I didn't understand at first , Want to put characters char Put it in the stack , But in this way Put the newly obtained elements on the stack It's very troublesome , If you put the structure directly into the stack, it can be easily implemented .
Here you can write a Constructors , Directly initialize the definition structure (C++ I learned it in school ), In that case, you can define one less tmp Structure .
边栏推荐
- 1010 things that college students majoring in it must do before graduation
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
- 【高老师软件需求分析】20级云班课习题答案合集
- STM32 how to use stlink download program: light LED running light (Library version)
- Matlab example: two expressions of step function
- 渗透测试 ( 2 ) --- 渗透测试系统、靶机、GoogleHacking、kali工具
- cs零基础入门学习记录
- Determine the Photo Position
- Perform general operations on iptables
- F - Birthday Cake(山东省赛)
猜你喜欢

1010 things that college students majoring in it must do before graduation

入门C语言基础问答

Information security - Analysis of security orchestration automation and response (soar) technology

Determine the Photo Position

信息安全-威胁检测引擎-常见规则引擎底座性能比较

渗透测试 ( 1 ) --- 必备 工具、导航

【练习-7】Crossword Answers

Web based photo digital printing website

渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus

Matlab example: two expressions of step function
随机推荐
Interesting drink
Opencv learning log 19 skin grinding
Record of brushing questions with force deduction -- complete knapsack problem (I)
Optimization method of path problem before dynamic planning
Web based photo digital printing website
对iptables进行常规操作
Perinatal Software Industry Research Report - market status analysis and development prospect forecast
Penetration test (3) -- Metasploit framework (MSF)
C 基本语法
入门C语言基础问答
差分(一维,二维,三维) 蓝桥杯三体攻击
最全编程语言在线 API 文档
洛谷P1102 A-B数对(二分,map,双指针)
数据在内存中的存储&载入内存,让程序运行起来
nodejs爬虫
C语言必背代码大全
【练习-10】 Unread Messages(未读消息)
STM32如何使用STLINK下载程序:点亮LED跑马灯(库版本)
Learning records: serial communication and solutions to errors encountered
Research Report on market supply and demand and strategy of geosynthetics industry in China