A. Ksenia and Pan Scales

来源:互联网 发布:08cms产品库报价系统 编辑:程序博客网 时间:2024/06/08 00:32
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|TL
output
AC|TL
input
|ABCXYZ
output
XYZ|ABC
input
W|TF
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
原创粉丝点击