当前位置:网站首页>[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 .
边栏推荐
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
- Optimization method of path problem before dynamic planning
- 0 - 1 problème de sac à dos (1)
- Learning record: use STM32 external input interrupt
- Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
- Matlab example: two expressions of step function
- Opencv learning log 16 paperclip count
- Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
- 入门C语言基础问答
- 【练习-8】(Uva 246)10-20-30==模拟
猜你喜欢

渗透测试 ( 4 ) --- Meterpreter 命令详解

X-Forwarded-For详解、如何获取到客户端IP

STM32 learning record: play with keys to control buzzer and led

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

Learning record: use STM32 external input interrupt

Penetration test (3) -- Metasploit framework (MSF)

MATLAB综合练习:信号与系统中的应用

Learning record: use stm32f1 watchdog

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

Determine the Photo Position
随机推荐
C语言是低级和高级的分水岭
【练习4-1】Cake Distribution(分配蛋糕)
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
初入Redis
渗透测试 ( 1 ) --- 必备 工具、导航
毕业才知道IT专业大学生毕业前必做的1010件事
China's earthwork equipment market trend report, technical dynamic innovation and market forecast
Cost accounting [20]
编程到底难在哪里?
Nodejs+vue online fresh flower shop sales information system express+mysql
Ball Dropping
Research Report of pharmaceutical solvent industry - market status analysis and development prospect prediction
Gartner: five suggestions on best practices for zero trust network access
Report on the market trend, technological innovation and market forecast of printing and decorative paper in China
通俗地理解什么是编程语言
Learning record: how to perform PWM output
C 基本语法
入门C语言基础问答
Alice and Bob (2021牛客暑期多校训练营1)
0 - 1 problème de sac à dos (1)