后进先出
栈顶可进出元素,栈顶不可
可用数组和一个记录栈顶位置的变量实现栈结构
例题:
#include<bits/stdc++.h>
using namespace std;
int f[1000005],sum[1000005],s1[1000005],s2[1000005];
int main()
{
int t;
f[0]=-0x3f3f3f3f;
while(~scanf("%d",&t))
{
int t1=0,t2=0;
while(t--)
{
char c[2];scanf("%s",&c);
if(c[0]=='I')
{
int x;scanf("%d",&x);
t1++;
s1[t1]=x;
sum[t1]=sum[t1-1]+x;
f[t1]=max(f[t1-1],sum[t1]);
}
else if(c[0]=='D')
{
if(t1) t1--;
}
else if(c[0]=='L')
{
int tem;
if(t1)
s2[++t2]=s1[t1--];
}
else if(c[0]=='R')
{
if(t2)
{
s1[++t1]=s2[t2--];
sum[t1]=sum[t1-1]+s1[t1];
f[t1]=max(f[t1-1],sum[t1]);
}
}
else if(c[0]=='Q')
{
int x;scanf("%d",&x);
printf("%d\n",f[min(t1,x)]);
}
}
}
}
进出栈序列问题
给定1-N这n个整数,每个数都要进栈并出栈一次,进栈顺序1,2~N,求可能的出栈序列。
1.求解具体的进出栈序列方案
搜索(递归):O(2N)
两种状态分支:下一个进栈;当前栈顶出栈
例题:火车进栈
#include<bits/stdc++.h>
#define ll long long
const int N=1e5+5;
using namespace std;
stack<int>s;
vector<int>a;
int n,res=20;
void dfs(int k)
{
if(res==0) return;
if(a.size()==n)
{
res--;
for(int i=0;i<a.size();++i)
cout<<a[i];
cout<<endl;
return;
}
if(s.size())
{
int t=s.top();
a.push_back(t);s.pop();
dfs(k);
s.push(t);
a.pop_back();
}
if(k<=n)
{
s.push(k);
dfs(k+1);
s.pop();
}
}
int main()
{
cin>>n;
dfs(1);
}
2.递推:O(N2)
不关心具体方案情况下计算出栈序列种类数
分解子问题:考虑‘1’在出栈序列中的位置,设为k
1.‘1’入栈
2. 2 ~ k 这 k-1 个数以某种序列进出栈
3. ‘1’ 出栈(第k位)
4. k+1 ~ N 这N-k个数以某种序列进出栈
故SN=Σk=1N Sk-1 *SN-k
3.动态规划:O(N2)
令F[i][j]表示有i个数未进栈,j个数在栈中,n-i-j个数已出栈的方案数
边界F[0][0]=1(所有数都已经出栈)
求:初始状态下所有数未入栈F[N][0]
两种状态分支:下一个进栈;当前栈顶出栈
F[i][j]=F[i-1][j+1]+F[i][j-1]
详解参考:N个元素的进出栈总数-方法转换-动态规划
#include<bits/stdc++.h>
#define ll long long
const int N=1005;
using namespace std;
ll a[N][N];
int n;
int main()
{
cin>>n;
for(int i=0;i<=n;++i)
{
for(int j=0;j<=n;++j)
{
a[i][j]=0;
}
}a[0][0]=1;//边界
for(int i=0;i<=n;++i)
a[0][i]=1;//当前已经没有数尚未进栈,有i个数在栈里,只能依次出栈,方案数唯一
for(int i=1;i<=n;++i)
{
for(int j=0;j<=n;++j)
{
if(j==0)//当前栈中没有数,无法出栈,只有一种状态转换即进栈一个数
a[i][j]=a[i-1][j+1];
else
a[i][j]=a[i-1][j+1]+a[i][j-1];
}
}
cout<<a[n][0]<<endl;
}
表达式计算
中缀表达式:常用
前缀表达式:波兰式
后缀表达式:逆波兰式
后缀表达式求值:
1.建立一个用于存数的栈,逐一扫描后缀表达式中的元素
(1)遇到数字,将数字入栈
(2)遇到运算符,取出栈顶两数字进行计算,结果入栈
2.扫描完成后,栈中恰好剩一个数,即后缀表达式的值
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
stack<int>num;
while(cin>>s)
{
if(s=="#") break;
if(s=="+")
{
int t1=num.top();num.pop();
int t2=num.top();num.pop();
num.push(t1+t2);
}
else if(s=="-")
{
int t1=num.top();num.pop();
int t2=num.top();num.pop();
num.push(t2-t1);
}
else if(s=="/")
{
int t1=num.top();num.pop();
int t2=num.top();num.pop();
if(t1!=0)
{
num.push(t2/t1);
}
}
else if(s=="*")
{
int t1=num.top();num.pop();
int t2=num.top();num.pop();
num.push(t1*t2);
}
else
{
int t=0;
for(int i=0;i<s.size();i++)
{
t*=10;t+=(s[i]-'0');
}
num.push(t);
}
}
if(!num.empty())
{
cout<<num.top()<<endl;
num.pop();
}
}
中缀表达式转后缀表达式:
1.建立一个用于存运算符的栈,逐一扫描该中缀表达式中的元素
(1)遇到数字,输出
(2)遇到左括号,入栈
(3)遇到右括号,不断取出栈顶元素并输出,直到栈顶为左括号,左括号出栈
(4)遇到运算符,只要栈顶符号的优先级不低于新符号,不断取出栈顶并输出,把新符号入栈
2.依次取出并输出栈中所有剩余符号
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
stack<char>op;
map<char,int>prior;
prior['*']=prior['/']=2;
prior['+']=prior['-']=1;
prior['(']=0;
while(cin>>s)
{
if(s=="#") break;
if(s=="+"||s=="-"||s=="/"||s=="*")
{
char t=s[0];
if(!op.empty())
{
while(!op.empty()&&prior[op.top()]>=prior[t])
{
cout<<op.top()<<' ';
op.pop();
}
}
op.push(t);
}
else if(s=="(")
{
op.push('(');
}
else if(s==")")
{
while(!op.empty()&&op.top()!='(')
{
cout<<op.top()<<' ';
op.pop();
}
if(op.top()=='(') op.pop();
}
else
{
cout<<s<<' ';
}
}
while(!op.empty())
{
cout<<op.top()<<' ';
op.pop();
}
}
单调栈:
栈内数据大小单调递增/递减
stack<int> st;
for (遍历数组)
{
if (栈空 || 栈顶元素大于等于当前比较元素)
{
入栈;
}
else
{
while (栈不空 && 栈顶元素小于当前元素)
{
栈顶元素出栈;
更新结果;
}
当前数据入栈;
}
}
例题:
柱状图中的最大矩形
#include<bits/stdc++.h>
using namespace std;
int a[100005],s[100005],w[100005];
int main()
{
int n;
while(cin>>n)
{
long long res=0;
if(n==0) break;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}
int p=0;a[n+1]=0;
for(int i=1;i<=n+1;++i)
{
if(a[i]>s[p])
{
s[++p]=a[i];w[p]=1;
}
else
{
int width=0;
while(s[p]>a[i])
{
width+=w[p];
res=max(res,(long long)width*s[p]);p--;
}
s[++p]=a[i];w[p]=width+1;
}
}
cout<<res<<endl;
}
}