codeforces题目之394 A. Counting Sticks

题目

A. Counting Sticks
time limit per test0.5 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
When new students come to the Specialized Educational and Scientific Centre (SESC) they need to start many things from the beginning. Sometimes the teachers say (not always unfairly) that we cannot even count. So our teachers decided to teach us arithmetics from the start. And what is the best way to teach students add and subtract? — That’s right, using counting sticks! An here’s our new task:

An expression of counting sticks is an expression of type:

[ A sticks][sign +][B sticks][sign =][C sticks] (1 ≤ A, B, C).
Sign + consists of two crossed sticks: one vertical and one horizontal. Sign = consists of two horizontal sticks. The expression is arithmetically correct if A + B = C.

We’ve got an expression that looks like A + B = C given by counting sticks. Our task is to shift at most one stick (or we can shift nothing) so that the expression became arithmetically correct. Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.

We really aren’t fabulous at arithmetics. Can you help us?

Input
The single line contains the initial expression. It is guaranteed that the expression looks like A + B = C, where 1 ≤ A, B, C ≤ 100.

Output
If there isn’t a way to shift the stick so the expression becomes correct, print on a single line “Impossible” (without the quotes). If there is a way, print the resulting expression. Follow the format of the output from the test samples. Don’t print extra space characters.

If there are multiple correct answers, print any of them. For clarifications, you are recommended to see the test samples.

Examples
input

||+|=|||||

output

|||+|=||||

input

|||||+||=||

output

Impossible

input

|+|=||||||

output

Impossible

input

||||+||=||||||

output

||||+||=||||||

Note
In the first sample we can shift stick from the third group of sticks to the first one.
In the second sample we cannot shift vertical stick from + sign to the second group of sticks. So we cannot make a - sign.
There is no answer in the third sample because we cannot remove sticks from the expression.
In the forth sample the initial expression is already arithmetically correct and that is why we don’t have to shift sticks.

题目理解

题目提供一个字符加法等式,当然,不一定正确。字符为’|’,计算等式左边的两个字符串的长度之和与等式右边的字符串的长度是否相同。通过挪动至多一个字符,从左到右,从右到左均可,使得等式成立。将得到的等式输出,如果得不到,则输出“Impossible”。

解题思路

理解题目可知,只有在左右等式的字符串长度相差为2或者是0的情况才可以获取等式成立的新等式。其他情况均不可能。

  1. 将整个等式字符串按‘=’和‘+’分割成三个等式,左1,左2和右字符串,获取三个子字符串的长度。进行判断
  2. 如果左1和左2的长度之和与右边字符串长度之差大于2或者等于1,则输出“Impossible”。
  3. 如果左1和左2的长度之和等于右边字符串长度,则直接输出原等式。
  4. 如果左右字符串长度相差为1,将长的一侧减去1,短的一侧加1。

代码如下(python)

string = input().strip()
a1, a2 = string.split('=')[0].split('+')
b = string.split('=')[1]
len_a = len(a1) + len(a2)

if abs(len_a - len(b)) > 2 or abs(len_a - len(b)) == 1:
    print('Impossible') 
elif len_a - len(b) == 0:
    print(string)
elif len(b) - len_a == 2:
    print("%s|+%s=%s"%(a1, a2, b[:-1]))
elif len(a1) == 1:
    print("%s+%s=%s|"%(a1, a2[:-1], b))
else:
    print("%s+%s=%s|"%(a1[:-1], a2, b))

代码解析

elif len(a1) == 1:
    print("%s+%s=%s|"%(a1, a2[:-1], b))
else:
    print("%s+%s=%s|"%(a1[:-1], a2, b))

做这个判断,是为了防止字符串只有一个字符,导致减掉一个字符后成为空字符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值