当前位置:网站首页>Niuke / Luogu - [noip2003 popularization group] stack
Niuke / Luogu - [noip2003 popularization group] stack
2022-07-26 00:05:00 【xyc20120615】
Background
Stack is the classic data structure in computer , To put it simply , A stack is a linear table that is limited to insert and delete operations at one end .
The stack has two most important operations , namely pop( Pop an element from the top of the stack ) and push( Stack an element ).
The importance of stacks is self-evident , Any course on data structure will introduce stacks . Ning Ning was reviewing the basic concept of stack , I think of a problem that is not mentioned in the book , And he can't give the answer himself , So I need your help .
Title Description

Ning Ning is thinking about such a problem : A sequence of operands ,1,2,\ldots ,n1,2,…,n( Shown here 1 To 3 The situation of ), Stack A The depth of is greater than nn.
Now you can do two things ,
- Count a number , Move from the header of the sequence of operands to the header of the stack ( Corresponding to the data structure stack push operation )
- Count a number , Move from the head of the stack to the end of the output sequence ( Corresponding to the data structure stack pop operation )
Use these two operations , From a sequence of operands, we can get a series of output sequences , The figure below shows 1 2 3 Generating sequences 2 3 1 The process of .

( The original state is shown in the figure above )
Your program will respond to a given n, Computes and outputs a sequence of operands 1,2,…,n The total number of possible output sequences after operation .
Input format
The input file contains only one integer n(1≤n≤18).
Output format
The output file has only one line , That is, the total number of possible output sequences .
I/o sample
Input #1
3Output #1
5explain / Tips
【 Title source 】
NOIP 2003 Third question of popularization group
Answer key :
So here I'm going to use theta dp The main yes Other methods will not , There are two situations : There is a number in the stack and there is no number in the stack , This procedure is very easy 了 .
Judgement procedure :
if(i>=1)// If there are a few in the station, the number that is not in the stack -1, Number in stack +1
dp[i][j]=dp[i-1][j]+dp[i+1][j-1];
if(i==0)// There are no numbers in the station , Can only enter the stack
dp[i][j]=dp[i+1][j-1];Program :
#include<bits/stdc++.h>
using namespace std;
int n,dp[20][20];
int main(){
cin>>n;
for(int i=0;i<=n;i++)
dp[i][0]=1;// Set boundary
for(int j=1;j<=n;j++)// Refers to the number of numbers that have not been stacked
for(int i=0;i<=n;i++){// Refers to the number of numbers in the stack
if(i>=1)// If there are a few in the station, the number that is not in the stack -1, Number in stack +1
dp[i][j]=dp[i-1][j]+dp[i+1][j-1];
if(i==0)// There are no numbers in the station , Can only enter the stack
dp[i][j]=dp[i+1][j-1];
}
cout<<dp[0][n];
return 0;
}边栏推荐
- 二叉树——617. 合并二叉树
- Yolov3 trains its own data set
- Storage of data in memory
- 模块二作业
- 行为型模式之观察者模式
- 栈与队列——347. 前 K 个高频元素
- Android solves the risk of database injection vulnerability
- Key features and application trends of next generation terminal security management
- [learning notes] unreal 4 engine introduction (III)
- LeetCode 沙胡同系列 -- 63. 不同路径 II
猜你喜欢
随机推荐
Lua脚本编写Wireshark插件解析第三方私有协议
栈与队列——347. 前 K 个高频元素
LeetCode高频题66. 加一,给你一个数组表示数字,则加1返回结果
Annotation @autowired source code analysis
Typescript TS basic knowledge and so on
行为型模式之观察者模式
Unexpected dubug tricks
模块二作业
二叉树——530.二叉搜索树的最小绝对差
SIGIR '22 recommendation system paper graph network
[learning notes] unreal 4 engine introduction (III)
[one library] mapbox GL! A map engine out of the box
Binary tree -- 257. All paths of binary tree
Practical experience of pair programming
Weight file and pre training file of yolov3
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
二叉树——617. 合并二叉树
Binary tree 101. Symmetric binary tree
Pytorch学习记录(一):Pytorch 简介
Yolov3 trains its own data set








