18933 括号匹配问题
#include <iostream>
#include <cstring>
#include <stack>
#include <string.h>
using namespace std;
typedef pair<int,int> PII;
const int N = 100010;
stack<int> s;
char str[N];
char ans[N];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len = strlen(str);
for(int i = 0 ; i < len ; i ++)
{
if(str[i]=='(')
{
s.push(i);
ans[i]=' ';
}
else if(str[i]==')')
{
if(!s.empty())
{
s.pop();
ans[i]=' ';
}
else ans[i] = '?';
}
else ans[i]=' ';
}
ans[len]='\0';
while(!s.empty())
{
int t = s.top();
ans[t] = '$';
s.pop();
}
puts(str);
puts(ans);
}
}
19009 后缀表达式
#include <iostream>
#include <cstring>
#include <stack>
#include <string.h>
using namespace std;
const int N = 100010;
stack<int> s;
char str[N];
char a[N];
void eval(char ch)
{
int b = s.top();
s.pop();
int a = s.top();
s.pop();
int x;
if(ch=='+') s.push(a+b);
else if(ch=='-') s.push(a-b);
else if(ch=='*') s.push(a*b);
else s.push(a/b);
}
int main()
{
char ch='0';
ch = getchar();
while(ch!='@')
{
if(ch-'0'>=0&&ch-'0'<10) s.push(ch-'0');
else if(ch==' ') ch='1';
else eval(ch);
ch = getchar();
}
cout<<s.top();
}
18932 出栈序列合法性判定
#include <iostream>
#include <cstring>
#include <stack>
#include <string.h>
using namespace std;
const int N = 100010;
int a[N],b[N];
stack<int> s;
int n;
int main()
{
cin>>n;
for(int i = 1 ; i <= n ; i ++) cin>>a[i];
for(int i = 1 ; i <= n ; i ++) cin>>b[i];
int i = 1 , k = 1;
while(k<=n)
{
if(i<=n)
{
if(a[i]!=b[k]) s.push(a[i++]);
else
{
i++;
k++;
}
}
else
{
if(s.top()==b[k++]) s.pop();
else
{
cout<<"no";
return 0;
}
}
}
cout<<"yes";
}
18715 出栈序列_sigd的博客-CSDN博客