Codeforces Round #733 (Div. 1 + Div. 2) E. Minimax 分情况讨论 + 思维

传送门

文章目录

题意:

给你一个串,你可以随意安排这个串,使得这个串的每个前缀的 k m p kmp kmp数组最大值最小,定义为 f ( a ) f(a) f(a),并且字典序最小,输出安排之后的串。
n ≤ 1 e 5 n\le1e5 n1e5

思路:

这个题就是个恶心的分情况讨论题,直接分情况吧。
( 1 ) (1) (1)全是一个字母的时候,直接输出即可。
( 2 ) (2) (2)有一个字母只有一个的时候,可以将最小的放在前面,其他的都依次接在他后面即可,这个时候 f ( a ) f(a) f(a) 0 0 0,字典序最小。
( 3 ) (3) (3)当有两个字母的时候,这个时候 f ( a ) f(a) f(a)最少为 1 1 1,我们就贪心的从头开始向后填字母,可以发现 a a b a b a b a aabababa aabababa这样是最优的,如果再多一个 a a a的话显然不能这么填了。所以当 c n t 2 > = c n t 1 − 2 cnt2>=cnt1-2 cnt2>=cnt12的时候,即可以用 b b b抵消掉多余的 a a a的时候,就可以这样填来消除。否则为了使 f ( a ) f(a) f(a)尽可能小,只能 a b b b b b a a a a abbbbbaaaa abbbbbaaaa这样填。
( 4 ) (4) (4)当有三个字母的时候,依旧采取上面的思想,类似于这样填 a a b a b a c a d a aababacada aababacada,也就是说如果除了 a a a之外的字母 n − c n t > = c n t − 2 n-cnt>=cnt-2 ncnt>=cnt2的话,就是可以这样填的,可知这样字典序最小,且 f ( a ) = 1 f(a)=1 f(a)=1。否则的话,因为有三个,所以可以这样填 a b a a a a a a c b abaaaaaacb abaaaaaacb,最后用 c c c将其隔开,防止填 b b b使得 f ( a ) = 2 f(a)=2 f(a)=2
最后分情况实现一下就好啦,由于把 X X X写成 Y Y Y还漏了第二种情况调了半天,真的是越来越不适合敲代码了。。

// Problem: E. Minimax
// Contest: Codeforces - Codeforces Round #733 (Div. 1 + Div. 2, based on VK Cup 2021 - Elimination (Engine))
// URL: https://codeforces.com/contest/1530/problem/E
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n;
char s[N];
int ne[N];
string ans;
int c[30];
vector<pair<int,char> >v;

void solve() {
	for(auto x:v) {
		for(int i=0;i<x.X;i++) ans+=x.Y;
	}
}

int main()
{
//	ios::sync_with_stdio(false);
//	cin.tie(0);

	int _; scanf("%d",&_);
	while(_--) {
		for(int i=0;i<26;i++) c[i]=0;
		scanf("%s",s+1); n=strlen(s+1);
		ans=""; v.clear();
		for(int i=1;i<=n;i++) c[s[i]-'a']++;
		int flag=false; int id=-1;
		for(int i=0;i<26;i++) if(c[i]) {
			v.pb({c[i],i+'a'});
			if(c[i]==1&&id==-1) id=(int)v.size()-1; 
		}
		if(id!=-1) {
			ans+=v[id].Y; v[id].X=0;
			solve();
		}
		else if(v.size()==1) {
			for(int i=0;i<v[0].X;i++) ans+=v[0].Y;
		} else if(v.size()==2) {
			int cnt1=v[0].X,cnt2=v[1].X;
			char ch1=v[0].Y,ch2=v[1].Y;
			if(cnt1==1) {
				ans+=ch1;
				while(cnt2) cnt2--,ans+=ch2;
			} else {
 				if(cnt2>=cnt1-2) {
 					ans+=ch1; ans+=ch1;
 					cnt1-=2;
 					int cnt=min(cnt1,cnt2);
 					cnt1-=cnt; cnt2-=cnt;
 					while(cnt) cnt--,ans+=ch2,ans+=ch1;
 					assert(cnt1==0);
 					while(cnt1) ans+=ch1,cnt1--;
 					while(cnt2) ans+=ch2,cnt2--;
 				} else {
 					cnt1--; ans+=ch1;
 					while(cnt2) ans+=ch2,cnt2--;
 					while(cnt1) ans+=ch1,cnt1--;
 				}
			}
		} else {
			int cnt=v[0].X; char ch=v[0].Y;
			if(cnt<=2) {
				while(cnt) cnt--,ans+=ch;
				v[0].X=0;
				solve();
			} else if(cnt==3) {
				v[0].X=0;
				ans+=ch; ans+=ch;
				ans+=v[1].Y; v[1].X--;
				ans+=ch;
				solve();
			} else {
				v[0].X=0;
				if(cnt-2<=n-cnt) {
					vector<char>all;
					for(auto x:v) { 
						for(int i=0;i<x.X;i++) all.pb(x.Y);
					}
					ans+=ch; ans+=ch; 
					cnt-=2;
					for(auto x:all) {
						ans+=x;
						if(cnt) cnt--,ans+=ch;
					}
				} else {
					ans+=ch; ans+=v[1].Y; v[1].X--;
					cnt--;
					while(cnt) ans+=ch,cnt--;
					ans+=v[2].Y; v[2].X--;
					solve();
				}
			}
		}
		cout<<ans<<endl;
	}




	return 0;
}
/*

*/









  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值