C. Naming Company

54 篇文章 0 订阅
20 篇文章 0 订阅

C. Naming Company
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.

To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of nletters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by nquestion marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.

For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :

Initially, the company name is ???.

Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.

Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.

Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.

In the end, the company name is oio.

Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?

A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)

Input

The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.

The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.

Output

The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.


这个题是个贪心。

首先就是我们把两个串排序,第一个串从小到大,第二个串从大到小。

然后选的个数是确定的,第一步策略就是:

1.第一个串选择(n+1)/2个,这里是整除,n为串的长度。第二个串就自然选择n/2个了。

然后是对于第二步策略。

2.当s[l1]<t[l2]或t[l2]>s[l1]时,这里的l指的是每个串的左端点的指针。这时候那么肯定是直接放在ans串的左边的。没有比这更优的了。

3.然后剩下一个比较重要的策略就是当s[l1]>=t[l2]或t[l2]<=s[l1]的情况的时候,我们肯定不能响当然的往左边放了,这时候我们应该尽量的让第二个往左放,所以此时我们应该占取右边的位置,在放置的时候当然是选择最大的放。

因为对于s来说就是小的往左放,大的往右放,而对于t来说就是正好相反的。

思路基本就是这样,然后看一下代码就懂了。

#include <bits/stdc++.h>

using namespace std;
const int MAXN=3e5+7;
char s[MAXN],t[MAXN],ans[MAXN];
int n,l1,l2,l,r1,r2,r;
bool cmp(char a,char b)
{
    return a>b;
}

int main()
{
    scanf("%s%s",s+1,t+1);
    n=strlen(s+1);
    l1=l2=l=1;
    r1=(n+1)/2;
    r2=n/2;
    r=n;
    sort(s+1,s+1+n);
    sort(t+1,t+1+n,cmp);
    while(l<=r)
    {
        if(s[l1]<t[l2])ans[l++]=s[l1++];
        else ans[r--]=s[r1--];
        if(l>r)break;
        if(t[l2]>s[l1])ans[l++]=t[l2++];
        else ans[r--]=t[r2--];
    }
    for(int i=1;i<=n;++i)printf("%c",ans[i]);
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值