Lex String 题解

思路:贪心

我们知道,肯定字典序越靠前的字符串,肯定越靠前的字母越小。

考虑每一次插入数组 cc 的字符,是在 a,ba,b 中没选过的最小的字母。

那么得先给 a,ba,b 数组排序。

这里可以先将 a,ba,b 转化成数字数组,更加方便以后的操作。记得输出 cc 数组时要用字符形式输出。

记得特判选某一个数组是否达到了 kk 次。如果达到了,只能选另外一个。

code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[101],b[101],c[101];
signed main()
{
	int T;
	cin>>T;
	while(T--)
	{
		string aa,bb;int k;bool x;int s=0;
		int len1,len2;
		cin>>len1>>len2>>k;
		cin>>aa>>bb;
		for(int i=1;i<=len1;i++) a[i]=aa[i-1];
		for(int i=1;i<=len2;i++) b[i]=bb[i-1];
		sort(a+1,a+len1+1);
		sort(b+1,b+len2+1);
		int len3=1,l1=1,l2=1;
		while(len3<=len1+len2&&l1<=len1&&l2<=len2)
		{
			if(s==k)
			{
				if(x==0) c[len3++]=b[l2++];
				else c[len3++]=a[l1++];
				x^=1,s=1;
			}
			else
			{
				if(a[l1]<b[l2])
				{
					c[len3++]=a[l1++];
					if(x==0) s++;
					else x=0,s=1;
				}
				else
				{
					c[len3++]=b[l2++];
					if(x==1) s++;
					else x=1,s=1;
				}
			}
		}
		for(int i=1;i<len3;i++) cout<<char(c[i]);
		cout<<endl;
	}
	return 0;
}
posted @ 2023-07-26 16:52  sLMxf  阅读(17)  评论(0)    收藏  举报  来源