Codeforces Round #224 (Div. 2) 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.

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;
}
posted @ 2018-04-16 21:34  友人-A  阅读(217)  评论(0编辑  收藏  举报