CodeForces - 724D

本文介绍了一种算法,用于从字符串中选择长度为m的子串,确保每个子串至少有一个字符被选中,并且最终选出的字符序列在字典序上是最小的。通过遍历字符串,每m个字符选取一次最小字符,遇到更大的字符则调整选择策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要保证每个长为m的子串有个位置被选掉,切选出的序列排序后字典序最小。

关键是要保证最大的字母字典序最小,所以先遍历,从前往后每m个选一个最小的,在相同字幕里选尽可能靠后的,保证之后字典序最小,然后找到无论如何必须要选的最大字母后,再从前忘后遍历,找出比最大字母小的字母全部选掉。

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define maxl 100010
#define inf 2000000001

using namespace std;

int m,anslen,len;
char ch[maxl],ans[maxl];
bool in[maxl];

void prework()
{
	scanf("%d",&m);
	scanf("%s",ch);	
}

void mainwork()
{
	int now=0,pos,mi,ma=0;
	len=strlen(ch);
	while(now+m-1<=len-1)
	{
		mi=inf;
		for(int i=now;i<=now+m-1;i++)
		if(ch[i]<=mi)
		{
			mi=ch[i];
			pos=i;
		}
		in[pos]=true;
		if(mi>ma) ma=mi; 
		now=pos+1;
	}
	for(int i=0;i<len;i++)
	if(ch[i]<ma && !in[i])
		in[i]=true;
	for(int i=0;i<len;i++)
	if(in[i])
		ans[++anslen]=ch[i];
	sort(ans+1,ans+1+anslen);
}

void print()
{
	for(int i=1;i<=anslen;i++)
		printf("%c",ans[i]);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值