A. Ksenia and Pan Scales

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



这是在Codeforces上提交的第一道题目,但是超时了,运行应该没有错误,但需要更好的算法来实现,正在进一步构思中。
这题目的意思是一个天平,两边分别放不同的字母,第二行有一些还没有放在天平上的字母,把第二行的字母全部放在天平上,保证天平平衡,若不能平衡,则输出 Impossible,若能平衡,则打印一种平衡后的天平,“|”为左右分界线。
#include <stdio.h>
#include <math.h>


int main()
{
    char tp[5],no[5],tpy[5],tpz[5];
    char *p = tp,*p2 = tpy,*p1 = tpz;
    char *pn = no;
    int i,j,k,n,m,x,y,z;

    while(gets(tp)!=NULL)
    {
        gets(no);
        p = tp;
        pn = no;
        p1 = tpz;
        p2 = tpy;
        i = 0;
        j = 0;
        k = 0;
        m = 0;
        while(*p != '\0')
        {
            if(*p == '|')
            {
                k = 1;
            }
            else if(k == 0)
            {
                *p1 = *p;
                p1++;
                i++;
            }
            else if(k == 1)
            {
                *p2 = *p;
                p2++;
                j++;
            }
            p++;
        }
        while(*pn != '\0')
        {
            m++;
            pn++;
        }
        if(abs(i - j) > m || (i == j && m % 2 == 1) || (m - abs(i - j))%2 == 1)
        {
            printf("Impossible\n");
        }
        else
        {
            if(i == j && m % 2 == 0)
            {
                while(m)
                {
                    pn--;
                    *p1 = *pn;
                    p1++;
                    i++;
                    m--;
                    pn--;
                    *p2 = *pn;
                    p2++;
                    j++;
                    m--;
                }
            }
            else if(abs(i - j) == m)
            {
                if(i > j)
                {
                    while(m--)
                    {
                        pn--;
                        *p2 = *pn;
                        p2++;
                        j++;
                    }
                }
                else
                {
                    while(m--)
                    {
                        pn--;
                        *p1 = *pn;
                        p1++;
                        i++;
                    }
                }
            }
            else
            {
                if(i > j)
                {
                    x = i - j;
                    while(x--)
                    {
                        pn--;
                        *p2 = *pn;
                        p2++;
                        j++;
                        m--;
                    }
                    while(m)
                    {
                        pn--;
                        *p1 = *pn;
                        p1++;
                        i++;
                        m--;
                        pn--;
                        *p2 = *pn;
                        p2++;
                        j++;
                        m--;
                    }
                }
                else
                {
                    x = j - i;
                    while(x--)
                    {
                        pn--;
                        *p1 = *pn;
                        p1++;
                        x++;
                        m--;
                    }
                    while(m)
                    {
                        pn--;
                        *p1 = *pn;
                        p1++;
                        i++;
                        m--;
                        pn--;
                        *p2 = *pn;
                        p2++;
                        j++;
                        m--;
                    }
                }
            }
            *p1 = '|';
            i++;
            for(x = 0; x < i; x++)
                printf("%c",tpz[x]);
            for(x = 0; x<j; x++)
                printf("%c",tpy[x]);
            printf("\n");
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值