最近搜索做的有些自闭 所以我来换种方式找虐了
ACWING.150 括号画家
https://www.acwing.com/problem/content/152/
#include <bits/stdc++.h> using namespace std; const int N=100010; stack<char> f; char s[N]; int main(){ int ans=0; cin>>s; int len=strlen(s); for(int i=0;i<len;i++){ while(!f.empty()) f.pop(); for(int j=i;j<len;j++){ if(s[j]=='(') f.push(')'); else if(s[j]=='[') f.push(']'); else if(s[j]=='{') f.push('}'); else if(f.empty()||s[j]!=f.top()) break; else { f.pop(); if(f.empty())ans=max(ans,j-i+1); } } } cout<<ans<<endl; return 0; }
ACWING.151 表达式计算
https://www.acwing.com/problem/content/153/
#include <bits/stdc++.h> using namespace std; stack <char> ops; stack <int> nums; void cal(){ int d=1; int a=nums.top();nums.pop(); int b=nums.top();nums.pop(); char c=ops.top();ops.pop(); if(c=='+') d=b+a; else if(c=='-') d=b-a; else if(c=='*') d=b*a; else if(c=='/') d=b/a; else { while(a--) d=d*b; } nums.push(d); } int main(){ string str,left; cin>>str; if(str[0]=='-') str='0'+str; int len=str.size(); for(int i=0;i<len;i++) left+='(';; str=left+str+')'; len=str.size(); for(int i=0;i<len;i++){ if(str[i]>='0'&&str[i]<='9'){ int j=i,t=0; while(str[j]>='0'&&str[j]<='9'){ t=t*10+str[j]-'0'; j++; } nums.push(t); i=j-1; } else { char c=str[i]; if(c=='(') ops.push(c); else if(c=='+'||c=='-'){ if(c=='-'&&i&&!(str[i-1]>='0'&&str[i-1]<='9')&&str[i-1]!=')'){ int j=i+1,t=0; while(str[j]>='0'&&str[j]<='9'){ t=t*10+str[j]-'0'; j++; } nums.push(-t); i=j-1; } else{ while(ops.top()!='(') cal(); ops.push(c); } } else if(c=='*'||c=='/'){ while(ops.top()=='*'||ops.top()=='/'||ops.top()=='^')cal(); ops.push(c); } else if(c=='^'){ while(ops.top()=='^') cal(); ops.push(c); } else if(c==')'){ while(ops.top()!='(') cal(); ops.pop(); } } } cout<<nums.top()<<endl; return 0; }
AcWing 131. 直方图中最大的矩形(单调栈)
https://www.acwing.com/problem/content/133/
#include <bits/stdc++.h> using namespace std; int n,p; long long ans; int a[100010],s[100010],w[100010]; int main(){ while(cin>>n,n){ ans=0;p=0; for(int i=1;i<=n;i++)cin>>a[i]; 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(a[i]<s[p]){ width+=w[p]; ans=max(ans,(long long)width*s[p]); p--; } s[++p]=a[i]; w[p]=width+1; } } cout<<ans<<endl; } return 0; }
ACWING.152 城市游戏
此题可以理解为一个二维的单调栈问题。
https://www.acwing.com/problem/content/154/
#include <bits/stdc++.h> using namespace std; int h[1010][1010],w[1010]; int s[1010]; int n,m; long long ans; long long work(int i){ long long res=0; memset(w,0,sizeof(w)); memset(s,0,sizeof(s)); h[i][m+1]=0; int p=0; for(int j=1;j<=m+1;j++){ if(h[i][j]>s[p]){ s[++p]=h[i][j]; w[p]=1; } else{ int width=0; while(s[p]>h[i][j]){ width+=w[p]; res=max(res,(long long)width*s[p]); p--; } s[++p]=h[i][j]; w[p]=width+1; } } return res; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char c; cin>>c; if(c=='F')h[i][j]=h[i-1][j]+1; else h[i][j]=0; } for(int i=1;i<=n;i++)ans=max(ans,work(i)); cout<<ans*3<<endl; return 0; }