Codeforces 551B ZgukistringZ

这是一道字符串处理题目

题目如下  点击打开链接

B. ZgukistringZ
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain a new one.

GukiZ has strings ab, and c. He wants to obtain string k by swapping some letters in a, so that k should contain as many non-overlapping substrings equal either to b or c as possible. Substring of string x is a string formed by consecutive segment of characters from x. Two substrings of string x overlap if there is position i in string x occupied by both of them.

GukiZ was disappointed because none of his students managed to solve the problem. Can you help them and find one of possible strings k?

Input

The first line contains string a, the second line contains string b, and the third line contains string c (1 ≤ |a|, |b|, |c| ≤ 105, where |s|denotes the length of string s).

All three strings consist only of lowercase English letters.

It is possible that b and c coincide.

Output

Find one of possible strings k, as described in the problem statement. If there are multiple possible answers, print any of them.

Examples
input
aaa
a
b
output
aaa
input
pozdravstaklenidodiri
niste
dobri
output
nisteaadddiiklooprrvz
input
abbbaaccca
ab
aca
output
ababacabcc
Note

In the third sample, this optimal solutions has three non-overlaping substrings equal to either b or c on positions 1 – 2 (ab), 3 – 4 (ab), 5 – 7 (aca). In this sample, there exist many other optimal solutions, one of them would be acaababbcc.

题意是 有三个字符串 a,b,c;

我们要对这第一个字符串进行操作,改编里面字符的顺序,但不改变个数,使得改编后的字符串x 中包含有个数尽量多的 b 和 c 字符串(是加起来最多的情况)

并且最后是b字符串在前,c字符串在后,最后再把剩下的多余的字符按ascii码顺序从小到大输出;

我的思路是先把每个字符串里的各种字符做一个统计,然后分别拿b和c去和a做对比,分别求出a中包含b的个数和a中包含c的个数;

然后根据这个最大值去循环它的个数 同时统计 b和c个数和的最大值;

最后输出b的同时删去a中使用了的字符,输出c时也同样操作,最后把剩余的字符输出

好的,上代码

#include<bits/stdc++.h>
using namespace std;
#define INF 0xFFFFFFF
int na[30],nb[30],nc[30],nk[30]; // 统计a,b,c中的字符 
char s1[100005],s2[100005],s3[100005];
int lin1,lin2; //用来记录 字符串的个数 
int t1,t2,t3;	//一个最大值的标记 
int main(){
	scanf("%s %s %s",s1,s2,s3);
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	int len3 = strlen(s3);
	for(int i = 0;i < len1;i++){
		na[s1[i] - 'a']++;
		nk[s1[i] - 'a']++;
	}	
	for(int i = 0;i < len2;i++){
		nb[s2[i] - 'a']++;
	}
	for(int i = 0;i < len3;i++){
		nc[s3[i] - 'a']++;
	}
	t1 = INF;
	for(int i = 0;i < 26;i++){
		if(nb[i] == 0)
			continue;
		t1 = min(na[i] / nb[i],t1);
	}
	t2 = INF;
	for(int i = 0;i < 26;i++){
		if(nc[i] == 0)
			continue;
		t2 = min(na[i] / nc[i],t2);
	}
	int ans = 0;
	lin1 = lin2 = 0;
	if(t1 > t2){
		for(int i = 1;i <= t1;i++){
			for(int j = 0;j < 26;j++){
				nk[j] = na[j];
			}
			for(int j = 0;j < len2;j++){
				nk[s2[j] - 'a'] -= i;
			}	
			t3 = INF;
			for (int j = 0; j < 26; j++) {
				if (nc[j] == 0) 
					continue;
				t3 = min(nk[j] / nc[j], t3);
			}
			if(t3 + i > ans){
				ans = t3 + i;
				lin1 = i;
				lin2 = t3;
			}
		}
	}else{
		for(int i = 1;i <= t2;i++){
			for(int j = 0;j < 26;j++){
				nk[j] = na[j];
			}
			for(int j = 0;j < len3;j++){
				nk[s3[j] - 'a'] -= i;
			}
			t3 = INF;
			for (int j = 0; j < 26; j++){
				if (nb[j] == 0) 
					continue;
				t3 = min(nk[j] / nb[j], t3);
			}
			if(t3 + i > ans){
				ans = t3 + i;
				lin1 = t3;
				lin2 = i;
			}
		}
	} 
	for(int i = 1;i <= lin1;i++){
		printf("%s",s2);
	}
	for (int i = 0; i < len2; i++){
		na[s2[i] - 'a'] -= lin1;
	}
	for(int i = 1;i <= lin2;i++){
		printf("%s",s3);
	}
	for (int i = 0; i < len3; i++){
		na[s3[i] - 'a'] -= lin2;
	}
	for (int i = 0; i < 26; i++){
		for (int j = 0; j < na[i]; j++) {
			printf("%c",i + 'a');
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值