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
? + ? - ? + ? + ? = 42
output
Possible
9 + 13 - 39 + 28 + 31 = 42
input
? - ? = 1
output
Impossible
input
? = 1000000
output
Possible
1000000 = 1000000

#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;
}



CodeForces 663A Rebus的更多相关文章

  1. Codeforces Round #347 (Div.2)_B. Rebus

    题目链接:http://codeforces.com/contest/664/problem/B B. Rebus time limit per test 1 second memory limit ...

  2. Codeforces Round #347 (Div. 2) B. Rebus

    题目链接: http://codeforces.com/contest/664/problem/B 题意: 给你一个等式,把等式左边的问号用1到n(n为等式右边的数)的数填好,使得等式成立 题解: 贪 ...

  3. codeforces 664B B. Rebus(乱搞题)

    题目链接: B. Rebus time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. 【16.05%】【codeforces 664B】Rebus

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. python导入模块的两种方式

    第一种 from support import * 这种方式导入后可以直接调用(有命名冲突问题)命名冲突后定义的覆盖前定义的 如果在函数导入前定义 则导入函数覆盖 否则相反 if __name__ = ...

  2. SICP 1.23-1.26体会

    1.23 代码修改非常easy, 关键是时间. 电脑上算了一下. 100000000下面全是0. 開始还以为代码写错了. 最后没办法, 用1e10 1e11来计算. 发现比 1e11 1e12快1.2 ...

  3. host文件配置 了解

    https://blog.csdn.net/CJF_iceKing/article/details/7702694 hosts文件位于" C:\Windows\System32\driver ...

  4. mysql数据库中不能插入0000-00-00 00:00:00日期数据(报错Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00')

    报错信息 SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' ...

  5. 点滴积累【C#】---C#实现上传word以流形式保存到数据库和读取数据库中的word文件。

    本文修改来源:http://www.cnblogs.com/zmgdpg/archive/2005/03/31/129758.html 效果: 数据库: 思路: 首先保存word到数据库:获取上传文件 ...

  6. NIO - Selector源码分析

    1. 背景 SelectableChannel对象的多路复用器. 可以通过调用Selector.open()方法创建Selector对象.Selector.open()方法会利用系统默认的Select ...

  7. Atitit.常用分区api的attilax总结

    Atitit.常用分区api的attilax总结 1. Api 来源与oracle与mysql1 1.1. 分区定义partition by range (uid)  使用VALUES LESS TH ...

  8. MyEclipse中背景颜色的设定

    设置代码编写区域背景色的方法: Window-->Preferences-->General-->Editors-->TextEditors-->在Appearance ...

  9. opencv读取图像输入到tensorflow模型中进行运算【cpp】

    void TransformMatToTensor(const cv::Mat &image, Tensor &input_tensor, int input_width, int i ...

  10. 1. DataBinding - offical tutorial

    1. DataBinding - offical tutorial android DataBinding tutorial 构建环境 数据与布局文件的绑定 data binding 表达式 数据对象 ...