一、vector(动态数组)
二、stack(栈)
1、表达式括号匹配
#include<bits/stdc++.h>
using namespace std;
int main()
{ char a;
stack <char> st;
do
{ a=getchar();
if(a=='(' ) st.push(a);
if(a==')')
if(st.empty()!=1) st.pop();
else break;
}while(a!='@');
if(st.empty()==1) cout<<"yes";
else cout<<"no";
}
2、字符串匹配
没有符号级的
#include<bits/stdc++.h>
using namespace std;
int main()
{ char a;
int n,i;
stack <char> st;
cin>>n;
a=getchar();//这个不能少,接收掉输入数字后的回车
for(i=1;i<=n;i++)
{
do
{ a=getchar();
if(a=='('||a=='{'||a=='['||a=='<' ) st.push(a);
else
switch(a)
{ case ')':if(!st.empty()&&st.top()=='(') st.pop();break;
case '}':if(!st.empty()&&st.top()=='{') st.pop();break;
case '>':if(!st.empty()&&st.top()=='<') st.pop();break;
case ']':if(!st.empty()&&st.top()=='[') st.pop();break;
}
}while(a!='\n');
if(st.empty()==1) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}
有符号级别的
#include<bits/stdc++.h>
using namespace std;
int a[10001]={0},b[10001]={0};char s[10001];
void gsd(char s[])
{ int i;
for(i=0;i<strlen(s);i++)
{ if (s[i]=='{') a[i+1]=1;
if (s[i]=='[') a[i+1]=2;
if (s[i]=='(') a[i+1]=3;
if (s[i]=='<') a[i+1]=4;
if (s[i]=='>') a[i+1]=5;
if (s[i]==')') a[i+1]=6;
if (s[i]==']') a[i+1]=7;
if (s[i]=='}') a[i+1]=8;
}
}
int main()
{ int n,i,t=0,j,m,k;
cin>>n;
for(i=1;i<=n;i++)
{ cin>>s; t=0; gsd(s);
for(j=1;j<=strlen(s);j++)
{ if(a[j]<=4)
if(a[j]>=b[t]) b[++t]=a[j];
else break;
if(a[j]>=5)
if(a[j]+b[t]==9) t--;
else t++;
}
if(t==0) cout<<"yes"<<endl;
else cout<<"NO"<<endl;
}
}
3、表达式求值
#include<bits/stdc++.h>
using namespace std;
int main() {
char str;
int flag1,flag2,ans=0;
cin>>flag1;
flag1%=10000;
while(cin>>str)
{ cin>>flag2;
flag2%=10000;
if(str=='+')
{ ans+=flag1;
ans%=10000;
flag1=flag2;
}
if(str=='*')
{ flag1*=flag2;
flag1%=10000;
}
}
ans+=flag1;
ans%=10000;
cout<<ans;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{ int a=0,b=0,sum=0;
char c;
stack <int> sn;
cin>>a;
sn.push(a%=10000);
while(cin>>c)
{ if(c=='\n') break;
cin>>b;
if(c=='+') sn.push(b%=10000);
if(c=='*') { b=b*sn.top()%10000;sn.pop();sn.push(b);}
}
while(!sn.empty())
{ sum+=sn.top();
sn.pop();
}
cout<<sum;
}
4、编程求一个后缀表达式的值
#include <bits/stdc++.h>
using namespace std;
stack<int> n;
char ch;
int s,x,y;
int main()
{ while(ch!='@')
{ ch=getchar();
switch(ch)
{ case '+':x=n.top();n.pop();y=n.top();n.pop();n.push(x+y);break;
case '-':x=n.top();n.pop();y=n.top();n.pop();n.push(y-x);break;
case '*':x=n.top();n.pop();y=n.top();n.pop();n.push(x*y);break;
case '/':x=n.top();n.pop();y=n.top();n.pop();n.push(y/x);break;
case '.':n.push(s);s=0;break;
default :s=s*10+ch-'0';break;
}
}
printf("%d\n",n.top());
return 0;
}
5、排队
#include<bits/stdc++.h>
using namespace std;
int main()
{ int n,a;
cin >> n;
long long ans = 0;
stack<int>s1;
for(int i = 0; i < n; i++){
cin >> a;
while(!s1.empty() && s1.top() <= a)
s1.pop();
ans += s1.size();
s1.push(a);
}
cout<<ans;
return 0;
}
6、车厢调度(train)
#include <bits/stdc++.h>
using namespace std;
int main()
{ stack <int> st;
int a[1001],i,n,cur=1;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{ while(cur<=a[i])
st.push(cur++);
cout<<st.size()<<endl;
if(a[i]==st.top()) st.pop();
else{cout<<"NO"<<endl; return 0;}
}
cout<<"YES";
}
7、溶液模拟器
#include<bits/stdc++.h>
using namespace std;
stack<int> stv;
stack<double> stc;
int main()
{ char ch;
int v0,v,n;
double c0,c;
scanf("%d %lf\n",&v0,&c0);
scanf("%d\n",&n);
stv.push(v0);stc.push(c0);
for(int i = 1; i <= n; i++){
scanf("%c",&ch);
if(ch == 'P'){
scanf("%d %lf",&v,&c);
v0 = stv.top();
c0 = stc.top();
stv.push(v0 + v);
stc.push((v0 * c0 + v * c) / stv.top());
printf("%d %.5f\n",stv.top(),stc.top());
}else{
if(stv.size() > 1){stv.pop();stc.pop();}
printf("%d %.5f\n",stv.top(),stc.top());
}
scanf("\n");
}
return 0;
}
8、火车进站出站问题
9、中缀表达式值(expr)
10、计算(calc)
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("cal.in","r",stdin);
freopen("cal.out","w",stdout);
char ch;
ch = getchar();
string s; s = ch; ch = getchar();
while(true){
stack<double>s1,s3;
stack<char>s2,s4;
while(ch != '\n') {s += ch;if ((ch = getchar()) == EOF) break;}
//std::cout << (int)s[0] << (int)s[1] << std::endl;
if ((s == "0")||(s == "0\r")) break;
s += ' ';
//printf(s.c_str());
long long temp = 0;
bool lastnumber = false;
for(int i = 0; i < s.size(); i++){
//printf("#%c %d#", s[i], (int)temp);
if(s[i] == ' '){
if (lastnumber) {
if(! s2.empty() && s2.top() == '*'){
double x1 = s1.top();
s1.pop();s2.pop();
x1 *= temp;
s1.push(x1);
}else
if(! s2.empty() && s2.top() == '/'){
double x1 = s1.top();
s1.pop();s2.pop();
x1 /= double(temp);
s1.push(x1);
}else
s1.push(double(temp));
}
temp = 0;
}else
if('0' <= s[i] && s[i] <= '9') { temp = temp * 10 + s[i] - '0';lastnumber = true;}
else {
s2.push(s[i]);
lastnumber = false;
}
}
while(! s1.empty()){ s3.push(s1.top()); s1.pop();}
while(! s2.empty()){ s4.push(s2.top()); s2.pop();}
while(! s4.empty()){
double x1 = s3.top(); s3.pop();
double x2 = s3.top(); s3.pop();
if(s4.top() == '+') s3.push(x1 + x2); else s3.push(x1 - x2);
s4.pop();
}
printf("%.2f\n",s3.top());
ch = getchar();
s = ch;
if ((ch = getchar()) == EOF) break;
}
return 0;
}