#codeforces #244 (div2)A. Ksenia and Pan Scales【模拟】

Ksenia has ordinary pan scales and several weights of an equal mass. Ksenia has already put some weights on the scales, while other weights are untouched. Ksenia is now wondering whether it is possible to put all the remaining weights on the scales so that the scales were in equilibrium.

The scales is in equilibrium if the total sum of weights on the left pan is equal to the total sum of weights on the right pan.

Input

The first line has a non-empty sequence of characters describing the scales. In this sequence, an uppercase English letter indicates a weight, and the symbol "|" indicates the delimiter (the character occurs in the sequence exactly once). All weights that are recorded in the sequence before the delimiter are initially on the left pan of the scale. All weights that are recorded in the sequence after the delimiter are initially on the right pan of the scale.

The second line contains a non-empty sequence containing uppercase English letters. Each letter indicates a weight which is not used yet.

It is guaranteed that all the English letters in the input data are different. It is guaranteed that the input does not contain any extra characters.

Output

If you cannot put all the weights on the scales so that the scales were in equilibrium, print string "Impossible". Otherwise, print the description of the resulting scales, copy the format of the input.

If there are multiple answers, print any of them.

Examples
Input
AC|T
L
Output
AC|TL
Input
|ABC
XYZ
Output
XYZ|ABC
Input
W|T
F
Output
Impossible
Input
ABC|
D
Output
Impossible
题意:为了使原字符串左右平衡(以|为分界),将给定的字符串加入原字符串中,如果无法加入,输出impossible

思路:纯模拟题。如果原字符串左右长度之和与给定字符串的长度奇偶性不一致,或者左右长度之差大于给定字符串长度,直接输出impossible,否则将左右字符串长度分别和需要达到的平衡长度进行比较输出,具体实现见注释~

#include<bits/stdc++.h>
using namespace std;
#define N 1000
char str[N+10],str2[N+10];
int main()
{
	int l1,l2,l3;
	while(scanf("%s",str)!=EOF)
	{
		scanf("%s",str2);
		l2 = strlen(str2);
		l1 = 0;
		for(int i = 0; str[i]!='\0';i ++)
		{
			if(str[i] == '|')
				break;
			l1 ++;
		}
		l3 = 0;
		for(int i = l1+1; str[i]!='\0';i ++)
			l3 ++;
		if((l3+l1)%2!=l2%2||fabs(l3-l1)>l2)//二者奇偶性不一致或者二者只差大于给定值 
			cout<<"Impossible"<<endl;
		else
		{
			
			for(int i = 0;str[i]!='|';i++)//先输出前半部分 
				printf("%c",str[i]);
			if(l1 < (l3+l2+l1)/2)//如果前半部分小于平均长度 
			{
				for(int i = 0; i < (l3+l2+l1)/2-l1; i ++)//再输出需要添加的部分 
					printf("%c",str2[i]);
				printf("%c",str[l1]);// 单独输出l 
				if(l3 < (l3+l2+l1)/2)//如果后半部分小于平均长度
				 {
				 	for(int i = l1+1; str[i]!='\0'; i ++)//先输出后半部分 
				 		printf("%c",str[i]);
				 	for(int i = (l3+l2+l1)/2-l1; str2[i]!='\0';i++)//在输出需要添加的部分 
				 		printf("%c",str2[i]);
				 	printf("\n");
				 }
				 else
				 {
				 	for(int i = l1+1; str[i]!='\0';i++)//正常输出后半部分 
				 		printf("%c",str[i]);
				 	printf("\n");
				 }
			}
			else
			{
				printf("%c",str[l1]);//正常输出前半部分 
				if(l3 < (l3+l2+l1)/2)//如果后半部分小于平均长度
				 {
				 	for(int i = l1+1; str[i]!='\0'; i ++)//先输出后半部分 
				 		printf("%c",str[i]);
				 	for(int i = (l3+l2+l1)/2-l1; str2[i]!='\0';i++)//在输出需要添加的部分 
				 		printf("%c",str2[i]);
				 	printf("\n");
				 }
				 else
				 {
				 	for(int i = l1+1; str[i]!='\0';i++)//正常输出后半部分 
				 		printf("%c",str[i]);
				 	printf("\n");
				 }
			}
		}
	}
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值