前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces 663A Rebus

CodeForces 663A Rebus

作者头像
ShenduCC
发布2018-04-26 17:08:21
6630
发布2018-04-26 17:08:21
举报
文章被收录于专栏:算法修养

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

代码语言:javascript
复制
? + ? - ? + ? + ? = 42

output

代码语言:javascript
复制
Possible
9 + 13 - 39 + 28 + 31 = 42

input

代码语言:javascript
复制
? - ? = 1

output

代码语言:javascript
复制
Impossible

input

代码语言:javascript
复制
? = 1000000

output

代码语言:javascript
复制
Possible
1000000 = 1000000
代码语言:javascript
复制
代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
char a[1000];
int n;
int num1,num2;
int b[1000];
int main()
{
    gets(a);
    int len=strlen(a);
    num1=0;num2=0;
    int cnt=1;int pos;int now=1;b[0]=1;
    for(int i=0;i<len;i++)
    {
        if(a[i]=='+') {b[cnt++]=1;now++;}
        if(a[i]=='-') {b[cnt++]=-1;now--;}
        if(a[i]=='=')
        {
            pos=i;
            break;
        }
    }
    n=0;
    for(int i=pos+1;i<len;i++)
    {
        if(a[i]<='9'&&a[i]>='0')
            n=n*10+a[i]-'0';
    }
    for(int i=0;i<cnt;i++)
    {
        while(now<n&&b[i]<n&&b[i]>0)
            now++,b[i]++;
        while(now>n&&b[i]>-n&&b[i]<0)
            now--,b[i]--;
    }
    if(now!=n)
    {
        printf("Impossible\n");
        return 0;
    }
    printf("Possible\n");
    int j=0;
    for(int i=0;i<len;i++)
    {
        if(a[i]!='?')
        printf("%c",a[i]);
        else
        {
            printf("%d",abs(b[j++]));
        }
    }
    cout<<endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档