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.

Examples
input

Copy
AC|T
L
output

Copy
AC|TL
input

Copy
|ABC
XYZ
output

Copy
XYZ|ABC
input

Copy
W|T
F
output

Copy
Impossible
input

Copy
ABC|
D
output

Copy
Impossible

题意:有两行字符串,如何把第二行的字符串加到第一行,使“|”左右两边字符个数相等,如果不能,输出Impossible

我的代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<limits.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
char ch[maxn],c[maxn];
char a[maxn],b[maxn];
int main()
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>ch;
cin>>c;
int l1=strlen(ch);
int l2=strlen(c);
int la=0,lb=0;
for(int i=0;;i++)
{
if(ch[i]=='|') break;
a[la++]=ch[i];
}
for(int i=la+1;i<l1;i++) b[lb++]=ch[i];
int La=la,Lb=lb;
if(l2<abs(la-lb)) cout<<"Impossible\n";
else
{
if((l1-1+l2)%2)
{
cout<<"Impossible\n";
}
else if(la==lb)
{
if(l2%2) cout<<"Impossible\n";
else
{
for(int i=0;i<=(l2-1)/2;i++) cout<<c[i];
cout<<a<<"|"<<b;
for(int i=(l2-1)/2+1;i<l2;i++) cout<<c[i];
cout<<endl;
}
}
else if(la>lb)
{
if(l2==la-lb) cout<<a<<"|"<<b<<c<<endl;
else
{
for(int i=0;i<(la+lb+l2)/2-la;i++) cout<<c[i];
cout<<a<<"|"<<b;
for(int i=(la+lb+l2)/2-la;i<l2;i++) cout<<c[i];
cout<<endl;
}
}
else if(la<lb)
{
if(l2==lb-la) cout<<a<<c<<"|"<<b<<endl;
else
{
for(int i=0;i<(l1-1+l2)/2-la;i++) cout<<c[i];
cout<<a<<"|"<<b;
for(int i=(l1-1+l2)/2-la;i<l2;i++) cout<<c[i];
cout<<endl;
}
}
}
return 0;
}

帆神的代码:

#include<stdio.h>
#include<string.h>
char a[1006],b[1006];
int main()
{
scanf("%s",a);
scanf("%s",b);
int la=strlen(a),lb=strlen(b);
int u;
for(int i=0;i<la;i++)
{
if(a[i]=='|')
u=i;
}
int x=u,y=la-u-1;//x—前半部分字符串的长度,y—后半部分字符串的长度
if(x>y&&lb>=x-y&&lb%2==(x-y)%2)
{
int p=(lb-x+y)/2;
for(int i=0;i<u;i++)
printf("%c",a[i]);
for(int i=0;i<p;i++)
printf("%c",b[i]);
printf("|");
for(int i=p;i<lb;i++)
printf("%c",b[i]);
for(int i=u+1;i<la;i++)
printf("%c",a[i]);
printf("\n");
}
else if(y>x&&lb>=y-x&&lb%2==(y-x)%2)
{
int p=(lb+x-y)/2;
for(int i=0;i<u;i++)
printf("%c",a[i]);
for(int i=p;i<lb;i++)
printf("%c",b[i]);
printf("|");
for(int i=u+1;i<la;i++)
printf("%c",a[i]);
for(int i=0;i<p;i++)
printf("%c",b[i]);
printf("\n");
}
else if(x==y&&lb%2==0)
{
int p=(lb+x-y)/2;
for(int i=0;i<u;i++)
printf("%c",a[i]);
for(int i=p;i<lb;i++)
printf("%c",b[i]);
printf("|");
for(int i=u+1;i<la;i++)
printf("%c",a[i]);
for(int i=0;i<p;i++)
printf("%c",b[i]);
printf("\n");
}
else
printf("Impossible\n");
return 0;
}

Codeforces Round #224 (Div. 2) A. Ksenia and Pan Scales的更多相关文章

  1. Codeforces Round #224 (Div. 2)

    题目:http://codeforces.com/contest/382 A Ksenia and Pan Scales 一个求天平是否能够平衡的题目...水题,注意一下结果的输出就行. #inclu ...

  2. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  3. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  4. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  5. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  6. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  7. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  8. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  9. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

随机推荐

  1. CMake Error: CMake was unable to find a build program corresponding to "Ninja".

    系统环境: $ lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:g ...

  2. idea忽略文件

  3. [don't have permission to access]的一个经典原因

    那就是 ..... SELinux ...... 几年前好像经历过这个恶梦.现在又经历了一回. 从Windows上传了一个目录,做一个apache的别名Alias, 结果总是没有权限. chmod 7 ...

  4. 一个Java例子,解释清楚注解的作用

    原文出处:码农登陆 写在前面 今天聊的是注解,但其实单纯说注解,注解本身没有任何的作用.简单说和注释没啥区别,而它有作用的原因是:注解解释类,也就是相关对代码进行解释的特定类.一般这些类使用反射是可以 ...

  5. IE6不兼容postion:fixed已解决

    将要设置postion:fixed的元素的css中添加以下代码: //如果想要是头部悬停,则以下代码即可. .customService{        position: fixed;bottom: ...

  6. Shell脚本中点号+文件名的作用

    熟悉linux是程序员必须具备的技能,水滴石穿,一点点学习吧. linux中一个文件是根据其是否具有执行属性来判断他是否可以直接运行的.就像windows下的exe一样. 如果我们要执行某一个文件,可 ...

  7. h5 plus/h5+规范使用,模块索引,教你如何去看h5+的手册

    最近看了下h5+规范的官网,开始觉得晦涩难懂,确实很乱,不过这也是基于我不理解的情况,终于艰难读完了,现在来分享下心得吧,基本看完文章,按我的方法,应该可以直接上手项目. 我准备的工具 hbuilde ...

  8. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  9. python 利用quick sort思路实现median函数

    # import numpy as np def median(arr): #return np.median(arr) arr.sort() return arr[len(arr)>>1 ...

  10. 进程退出exit、_exit、abort

    分为正常退出,异常退出 正常退出的方法: 1.在main函数中执行return 2.调用exit函数 3.调用_exit  函数 ----------------------------------- ...