您的位置:首页 > 其它

CF:Problem 394A - Counting Sticks字符串处理(substr函数)

2014-02-22 13:41 316 查看
A. Counting Sticks

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard 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.

Sample test(s)

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.

这题理解错了……

Note that we cannot remove the sticks from the expression, also we cannot shift the sticks from the signs + and =.这句话没理解啊,英语好烂!!

以前用到过substr函数,那时觉得很有用,那时也觉得记住了,没想到忘了,所以还是写这篇以防下次又忘了……

substr函数:比如:string s="abcdef",string a=s.substr(起始位置,获取长度),注意的是起始位置是从0开始的。如:a=s.substr(1,4)="bcde".

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <list>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define pri(a) printf("%d\n",a)
#define f(i,a,n) for(i=a;i<n;i++)
#define F(i,a,n) for(i=a;i<=n;i++)
#define MM 1000002
#define MN 3005
#define INF 16843009000000
using namespace std;
typedef long long ll;
int main()
{
    string s,s1,s2,s3;
    int a,b,c,d;
    cin>>s;
    a=s.find("+"); //"+"的左边有多少个"|"
    s1=s.substr(0,a); //取出"+"左边的所有"|",下同
    b=s.find("=")-a-1;
    s2=s.substr(a+1,b);
    c=a+b; //左边总共的"|"
    d=s.size()-s.find("=")-1; //右边总共的"|"
    s3=s.substr(s.find("=")+1,d);
    if(c==d) cout<<s<<endl;
    else if(abs(c-d)!=2) puts("Impossible");
    else if(abs(c-d)==2)
    {
        if(c-d==2)
        {
            if(a==1) cout<<s1<<'+'<<s2.substr(0,b-1);
            else cout<<s1.substr(0,a-1)<<'+'<<s2;
            cout<<'='<<s3<<'|'<<endl;
        }
        else cout<<s1<<"|+"<<s2<<'='<<s3.substr(0,d-1)<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: