B. 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

做了好长时间,一点一点的思维向最优解靠拢,终于A掉了QAQ,o(≧v≦)o~~好棒。

#include <cstdio>
#include <queue>
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const long long inf = 1e9;
const int MAXN = 1e5+7;
int n;
char s[110];
char ans[110];
int num[110];
int main()
{
    int cnt = 0,num1 = 0,num2 = 0;
    while(~scanf("%s",s))
    {
        if(s[0] == '=')break;
        if(s[0] == '+')
        {
            ans[cnt++] = '+';
            num1++;
        }
        else if(s[0] == '-')
        {
            ans[cnt++] = '-';
            num2++;
        }
    }
    scanf("%d",&n);
    if(num1 == 0 && num2>0)puts("Impossible");//只有减号
    else if(num1 == num2 && num1 == 0)printf("Possible\n%d = %d\n",n,n);//没有符号
    else if(num1>0 && num2==0)//只有加号
    {
        if(num1>n-1)puts("Impossible");
        else
        {
            puts("Possible");
            printf("%d ",n-cnt);
            for(int i = 0; i < cnt ; ++i)
            {
                printf("%c 1 ",ans[i]);
            }
            printf("= %d\n",n);
        }
    }
    else//+-混合,一开始其实就是一个+号的
    {
        num1++;
        if(num1>num2)
        {
            if(num2*n+n<num1)//一种极限情况
            {
                puts("Impossible");
                return 0;
            }
            //如果在极限情况内的话,说明必定存在一种分配方案,这里采用均匀分配是最好的
            int dert = n + num2*n;
            int x = dert/num1 ;
            int y = dert%num1;
            if(!x)
            {
                puts("Impossible");
                return 0;
            }
            puts("Possible");
            if(y)
            {
                printf("%d ",x+1);
                y--;
            }
            else
            {
                printf("%d ",x);
            }
            for(int i = 0; i < cnt ; ++i)
            {
                printf("%c ",ans[i]);
                if(ans[i] == '-')
                {
                    printf("%d ",n);
                }
                else
                {
                    if(y)
                    {
                        printf("%d ",x+1);
                        y--;
                    }
                    else
                    {
                        printf("%d ",x);
                    }
                }
            }
            printf("= %d\n",n);
        }
        else
        {
            if(num1*n-n<num2)
            {
                puts("Impossible");
                return 0;
            }

            int dert = num1*n - n;
            int x = dert/num2 ;
            int y = dert%num2;
            if(!x)
            {
                puts("Impossible");
                return 0;
            }
            printf("Possible\n%d ",n);
            for(int i = 0; i < cnt ; ++i)
            {
                printf("%c ",ans[i]);
                if(ans[i] == '+')
                {
                    printf("%d ",n);
                }
                else
                {
                    if(y)
                    {
                        printf("%d ",x+1);
                        y--;
                    }
                    else
                    {
                        printf("%d ",x);
                    }
                }
            }
            printf("= %d\n",n);
        }

    }
    return 0;

}




在IDEA的java代码<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="admin" type="com.entil.Admin"/> <typeAlias alias="users" type="com.entity.Users" /> <typeAlias alias="banner" type="com.entity.Banner" /> <typeAlias alias="article" type="com.entity.Article" /> <typeAlias alias="programs" type="com.entity.Programs" /> <typeAlias alias="house" type="com.entity.House" /> <typeAlias alias="money" type="com.entity.Money" /> <typeAlias alias="pays" type="com.entity.Pays" /> <typeAlias alias="serves" type="com.entity.Servs" /> <typeAlias alias="orders" type="com.entity.Orders" /> <typeAlias alias="broken" type="com.entity.Broken" /> <typeAlias alias="complains" type="com.entity.Complains" /> <typeAlias alias="bbs" type="com.entity.Bbs" /> <typeAlias alias="rebus" type="com.entity.Rebbs" /> </typeAliases> <mappers> <mapper resource="mapper/admin.xml" /> <mapper resource="mapper/users.xml" /> <mapper resource="mapper/banner.xml" /> <mapper resource="mapper/article.xml" /> <mapper resource="mapper/programs.xml" /> <mapper resource="mapper/house.xml" /> <mapper resource="mapper/money.xml" /> <mapper resource="mapper/pays.xml" /> <mapper resource="mapper/serves.xml" /> <mapper resource="mapper/orders.xml" /> <mapper resource="mapper/broken.xml" /> <mapper resource="mapper/complains.xml" /> <mapper resource="mapper/bbs.xml" /> <mapper resource="mapper/rebus.xml" /> </mappers> </configuration>你怎么看
04-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值