time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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,如果是减号则为-1;

这个时候,如果总和小于所需的;

则把大于0的数字递增直到这个数字等于n或者已经达到了要求;小于0的则不能动(前面是负号);

如果总和大于所需的;

则把数字小于0的再减少一点(当然也不能小于-n);

这样可以递减总和。

具体的看代码;

如果以上操作都不能满足总和为n则输出无解信息;

一开始的数字初始化很巧妙;

#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long using namespace std; const int MAXN = 2000; char t;
int a[2000] = {0},now = 0,total = 1,n; void input_LL(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
a[++now] = 1;
while (cin>>t && t!='=')
{
if (t == '+')
a[++now] = 1,total++;
else
if (t== '-')
a[++now] = -1,total--;
}
input_int(n);
for (int i = 1;i <= now;i++)
if (a[i] > 0)
{
while (a[i]<n && total < n)
a[i]++,total++;
}
else
if (a[i] < 0)
{
while (a[i]>-n && total > n)
a[i]--,total--;
}
if (total!=n)
puts("Impossible");
else
{
puts("Possible");
printf("%d ",a[1]);
for (int i =2;i <= now;i++)
printf("%c %d ",a[i]>0?'+':'-',abs(a[i]));
printf("= %d\n",n);
}
return 0;
}

最新文章

  1. 利用ajax的方式来提交数据到后台数据库及交互功能
  2. Redis核心知识之—— 时延问题分析及应对、性能问题和解决方法【★★★★★】
  3. jsb游戏闪退 ScriptingScore::executeFunctionWithOwner 出错
  4. jQuery 、js 设置 显示隐藏
  5. [BZOJ 1500] [NOI2005] 维修数列
  6. java学习面向对象之static内存图解
  7. 借助github搭建自己的博客
  8. 使用Jekyll搭建免费的Github Pages个人博客
  9. maven本地jar
  10. 5. SQL Server数据库性能监控 - 当前请求
  11. C# 实现屏幕键盘 (ScreenKeyboard)
  12. ArcGIS制图表达Representation实战篇3-控制点
  13. POJ 2195 Going Home (带权二分图匹配)
  14. 2018-01-28-M个本地工作点代码同步到N个GIT远端
  15. for 循环中的 i 变量问题
  16. 【python3基础】相对路径,‘/’,‘./’,‘../’
  17. Game Engine Architecture 2
  18. Java知多少(80)图形界面设计基础
  19. Unity 资源的优化管理 学习
  20. C++中文件读写的操作

热门文章

  1. java中volatile关键字的含义--volatile并不能做到线程安全
  2. python3 pygame 坦克自动移动
  3. 杭州&quot;人才新政22条&quot; 硕士来杭工作一次性补贴2万元
  4. NSNotificationCenter消息通信(KVO)
  5. Linux定时器的使用(三种方法)
  6. The behavior of App killed or restored by Android System or by users
  7. 算法求解中的变量、数组与数据结构(STL 中的容器)
  8. jQuery 中 is() 函数常见使用方法
  9. composer 安装 laravel 更换下载源
  10. ios开发零散知识点总结