您的位置:首页 > 其它

Codeforces Round #224 (Div. 2)A. Ksenia and Pan Scales

2014-01-18 10:43 423 查看
A. Ksenia and Pan Scales

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Sample test(s)

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。如果天平上的砝码数和可以添加的砝码数之和不能被2整除,那就也是Impossible。最后,把天平需要达到平衡的砝码数记下,然后分配砝码即可。代码如下:

#include<stdio.h>
#include<string.h>
#include<math.h>
char s1[300];
char s2[300];
int main(){
int a,b,i,j,flag,len1,len2,mid;
scanf("%s",s1);
len1=strlen(s1);
a=0;b=0;flag=0;
for(i=0;i<len1;i++){
if(s1[i]=='|'){
flag=1;
continue;
}
if(flag)
b++;
else
a++;
}
scanf("%s",s2);
len2=strlen(s2);
if(abs(a-b)>len2)
printf("Impossible\n");
else if((a+b+len2)%2==0){
mid=(a+b+len2)/2;
for(i=0;i<a;i++)
printf("%c",s1[i]);
for(;i<mid;i++)
printf("%c",s2[i-a]);
printf("|");
for(j=0;j<b;j++)
printf("%c",s1[a+j+1]);
for(j=mid-a;j<len2;j++)
printf("%c",s2[j]);
printf("\n");
}
else
printf("Impossible\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 基础算法