Codeforces Round #224 (Div. 2) A - Ksenia and Pan Scales

本文解析了一道CodeForces竞赛题目,涉及平衡秤问题。通过输入字符串调整重量分布,实现左右两边重量相等的目标。文章提供了详细的解题思路及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
第一题没过,可能是隔了好几天没做了吧,今天又搞了几个小时还是没做出来
最后还是看了别人答案才懂的
题意其实很简单
给出两串字符,判断能否在‘|’两侧使得字符个数相等且包含所有给出的字符
我的想法是分类讨论
怎么讨论总有错的地方
贴上答案看电视剧去
#include <iostream>
#include <string>
using namespace std;
int main(void){
    string a,b;
    char a1[1000],b1[1000];
    int i,j,k,t1,t2;
    while(cin >> a >> b){
     int l1 = a.size();
     int l2 = b.size();
     int x = l1+l2-1;
     t1 = 0, t2 = 0;
     if(x%2){
      cout << "Impossible" << endl;
      continue;
     }
     x /= 2;
     for(i=0; i<l1; i++){
      if(a[i] == '|')
       break;
      a1[t1]=a[i];
      t1++;
     }
     x -= t1;
     for(j=i+1; j<l1; j++){
      b1[t2++] = a[j];
     }
     for(k=0; k<x; k++){
      a1[t1] = b[k];
      t1++;
     }
     for(j=k; j<l2; j++){
      b1[t2] = b[j];
      t2++;
     }
     if(t1-t2)
      cout << "Impossible" << endl;
     else{
      for(i=0; i<t1; i++)
       cout << a1[i];
      cout << "|";
      for(i=0; i<t2; i++)
       cout << b1[i];
     }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值