A. Ksenia and Pan Scales

来源:互联网 发布:知君本无邪txt 编辑:程序博客网 时间:2024/06/16 04:04

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|TL
output
AC|TL
input
|ABCXYZ
output
XYZ|ABC
input
W|TF
output
Impossible
input
ABC|D
output
Impossible


解题说明:先把左右盘的砝码数提取出来,如果两盘砝码数之差大于可以添加的砝码,就是Impossible。如果天平上的砝码数和可以添加的砝码数之和不能被2整除,那就也是Impossible。最后,把天平需要达到平衡的砝码数记下,然后分配砝码即可。

#include<iostream>#include<cstdio>#include<cmath>#include<cstdlib>#include <algorithm>#include<cstring>using namespace std;int main(){int i, res, j;char Str[30], extra[30];scanf("%s %s", Str, extra);res = (strlen(Str) + strlen(extra) - 1) / 2;for (i = 0; Str[i] != '|'; ++i);if ((strlen(Str) + strlen(extra) - 1) % 2 == 1 || i>res || (strlen(Str) - i - 1)>res){printf("Impossible\n");return 0;}for (j = 0; j<res - i; ++j){printf("%c", extra[j]);}printf("%s", Str);for (j = res - i; j<strlen(extra); ++j){printf("%c", extra[j]);}printf("\n");return 0;}


0 0
原创粉丝点击