侧边栏壁纸
  • 累计撰写 192 篇文章
  • 累计创建 2 个标签
  • 累计收到 87 条评论

【题解】【上海】【出栈序列】

Allen Best
2023-08-07 / 0 评论 / 1 点赞 / 92 阅读 / 417 字
温馨提示:
本文最后更新于 2023-08-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1E5 + 10; 
string s;
int n;
char a[MAXN]; 
int main(){
	cin >> n >> s;
	a[n - 1] = s[n - 1];
	for (int i = n - 2; i >= 0; --i)
		a[i] = min(s[i], a[i + 1]);//寻找字符串各个位置的最小子序列
	stack<char>stk;
	stk.push(s[0]);
	int inc = 1;
	while (!stk.empty() || inc < n){
		if (stk.empty() || inc < n && stk.top() > a[inc]){
			stk.push(s[inc++]);
		} else{
			cout << stk.top();
			stk.pop();
		}
	}
	cout << endl;
	return 0;	
}
0

评论区