codeforces_664B. Rebus

B. Rebus
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rebus of form ? + ? - ? + ? = n, consisting of only question marks, separated by arithmetic operation '+' and '-', equality and positive integer n. The goal is to replace each question mark with some positive integer from 1 to n, such that equality holds.

Input

The only line of the input contains a rebus. It's guaranteed that it contains no more than 100 question marks, integer n is positive and doesn't exceed 1 000 000, all letters and integers are separated by spaces, arithmetic operations are located only between question marks.

Output

The first line of the output should contain "Possible" (without quotes) if rebus has a solution and "Impossible" (without quotes) otherwise.

If the answer exists, the second line should contain any valid rebus with question marks replaced by integers from 1 to n. Follow the format given in the samples.

Examples
input
? + ? - ? + ? + ? = 42
output
Possible
9 + 13 - 39 + 28 + 31 = 42
input
? - ? = 1
output
Impossible
input
? = 1000000
output
Possible
1000000 = 1000000
 
     
先初始化所有减数为1,所有加数为 减数的和加上n 再除以 加数的个数,然后再根据情况进行调整
 
     
 
     
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

struct node
{
    int val,i;
    char l,r;
}a[210];

char c[1010];

int main()
{
    int i=0,j,n=0,top=0;
    char ch;
    gets(c);
    int len=strlen(c);
    for(i=0;c[i]!='=';i++)
    {
        if(c[i]=='?')
        {
            top++;
        }
        else if(c[i]=='+')
        {
            a[top-1].r='+';
            a[top].l='+';
        }
        else if(c[i]=='-')
        {
            a[top-1].r='-';
            a[top].l='-';
        }
    }
    a[top-1].r='=';
    for(;c[i]!='\0';i++)
    {
        if(c[i]>='0'&&c[i]<='9')
        {
            n=n*10+c[i]-'0';
        }
    }
    if(top==1)
    {
        printf("Possible\n");
        printf("%d = %d",n,n);
        return 0;
    }
    int mius=0;
    for(i=0;i0)
    {
        for(i=0;in||a[i].val==0)
        {
            Ps("Impossible");
            return 0;
        }
    }
    Ps("Possible");
    for(i=0;i


你可能感兴趣的:(cf)