Codeforces Round #224 (Div. 2)A. Ksenia and Pan Scales
2014-01-18 10:43
423 查看
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
output
input
output
input
output
input
output
解题报告:
此题说的是天平平衡问题。首先明白几点:第一、你要使天平能达到平衡;第二、天平上的”砝码“是不能动的;第三、天平左右都可以加“砝码”;
本人的思路是:先把左右盘的砝码数提取出来,如果两盘砝码数之差大于可以添加的砝码,就是Impossible。如果天平上的砝码数和可以添加的砝码数之和不能被2整除,那就也是Impossible。最后,把天平需要达到平衡的砝码数记下,然后分配砝码即可。代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
char s1[300];
char s2[300];
int main(){
int a,b,i,j,flag,len1,len2,mid;
scanf("%s",s1);
len1=strlen(s1);
a=0;b=0;flag=0;
for(i=0;i<len1;i++){
if(s1[i]=='|'){
flag=1;
continue;
}
if(flag)
b++;
else
a++;
}
scanf("%s",s2);
len2=strlen(s2);
if(abs(a-b)>len2)
printf("Impossible\n");
else if((a+b+len2)%2==0){
mid=(a+b+len2)/2;
for(i=0;i<a;i++)
printf("%c",s1[i]);
for(;i<mid;i++)
printf("%c",s2[i-a]);
printf("|");
for(j=0;j<b;j++)
printf("%c",s1[a+j+1]);
for(j=mid-a;j<len2;j++)
printf("%c",s2[j]);
printf("\n");
}
else
printf("Impossible\n");
return 0;
}
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
解题报告:
此题说的是天平平衡问题。首先明白几点:第一、你要使天平能达到平衡;第二、天平上的”砝码“是不能动的;第三、天平左右都可以加“砝码”;
本人的思路是:先把左右盘的砝码数提取出来,如果两盘砝码数之差大于可以添加的砝码,就是Impossible。如果天平上的砝码数和可以添加的砝码数之和不能被2整除,那就也是Impossible。最后,把天平需要达到平衡的砝码数记下,然后分配砝码即可。代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
char s1[300];
char s2[300];
int main(){
int a,b,i,j,flag,len1,len2,mid;
scanf("%s",s1);
len1=strlen(s1);
a=0;b=0;flag=0;
for(i=0;i<len1;i++){
if(s1[i]=='|'){
flag=1;
continue;
}
if(flag)
b++;
else
a++;
}
scanf("%s",s2);
len2=strlen(s2);
if(abs(a-b)>len2)
printf("Impossible\n");
else if((a+b+len2)%2==0){
mid=(a+b+len2)/2;
for(i=0;i<a;i++)
printf("%c",s1[i]);
for(;i<mid;i++)
printf("%c",s2[i-a]);
printf("|");
for(j=0;j<b;j++)
printf("%c",s1[a+j+1]);
for(j=mid-a;j<len2;j++)
printf("%c",s2[j]);
printf("\n");
}
else
printf("Impossible\n");
return 0;
}
相关文章推荐
- Codeforces Round #224 (Div. 2) A. Ksenia and Pan Scales
- #codeforces #244 (div2)A. Ksenia and Pan Scales【模拟】
- Codeforces Round #224 (Div. 2) D. Ksenia and Pawns
- 【Codeforces Round 324 (Div 2)E】【贪心 构造】Anton and Ira 全排列交换 最小距离成本
- Codeforces Round #250 (Div. 2)——The Child and Set
- Codeforces Round #369 (Div. 2) B. Chris and Magic Square
- Codeforces Round #199 (Div. 2) C. Cupboard and Balloons
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots
- Codeforces Round #389 (Div. 2)A. Santa Claus and a Place in a Class(水题)
- Codeforces Beta Round #61 (Div. 2) E. Petya and Post
- Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined)
- Codeforces Round #305 (Div. 2)B. Mike and Fun
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 排序
- Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry
- 想法题——Codeforces Round #190 (Div. 2)——B. Ciel and Flowers
- Codeforces Round #291 (Div. 2) Han Solo and Lazer Gun
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem
- 【Codeforces Round 370 (Div 2) E】【线段树 等比数列 区间合并】Memory and Casinos 赌场区间[l,r] l进r先出的概率
- Codeforces Round #370 (Div. 2) C. Memory and De-Evolution【dfs+想法】
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree(dfs+最大公约数+因子+树)