1-02E. JM的西伯利亚特快专递

1-02E. JM的西伯利亚特快专递

题解

题面说得很明白了,很明显的一道贪心。

我们知道要让字典序最小,我们必须让越前面的数越小。所以,贪心思想如下:

我们每次假定当前s串中最小的值,若t串尾部的值比这更小,那么就要t串尾的值,否则一直取到最后一个。

其正确性很容易证明。

源码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#define MAXN 100005
using namespace std;
typedef long long LL;
char str[MAXN];
int a[MAXN],n,sum[30],b[MAXN],tot;
int ans[MAXN],cnt;
#define gc() getchar()
template<typename _T>
inline void read(_T &x){
	_T f=1;x=0;char s=gc();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=gc();}
	while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=gc();}
	x*=f;
} 
signed main() {
	scanf("%s",str);n=(int)strlen(str);
	for(int i=0;i<n;i++)a[i+1]=str[i]-'a'+1;
	for(int i=1;i<=n;i++)sum[a[i]]++;
	int ind=1;
	for(int i=1;i<=n;i++){
		while(!sum[ind])ind++;
		while(tot&&b[tot]<=ind)ans[++cnt]=b[tot--];
		if(a[i]==ind)ans[++cnt]=ind,sum[ind]--;
		else b[++tot]=a[i],sum[a[i]]--;
	}
	ind=27;
	while(tot&&b[tot]<=ind)ans[++cnt]=b[tot--];
	for(int i=1;i<=cnt;i++)printf("%c",ans[i]+'a'-1);
	return 0;
}

谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值