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. 使用typescript开发js代码提升代码维护性

    function test() { var gs = new Greeter('ss'); alert( gs.greet()); } window.onload = test; class Gree ...

  2. [iOS]delegate和protocol

    转自:http://haoxiang.org/2011/08/ios-delegate-and-protocol/ 今天上班和同事讨论工程怎么组织的时候涉及到这个话题.iOS开发上对delegate使 ...

  3. Linux高性能server编程——I/O复用

     IO复用 I/O复用使得程序能同一时候监听多个文件描写叙述符.通常网络程序在下列情况下须要使用I/O复用技术: client程序要同一时候处理多个socket client程序要同一时候处理用户 ...

  4. C++语言基础(20)-模板的非类型参数

    一.在函数模板中使用非类型参数 #include <iostream> using namespace std; template<class T> void Swap(T & ...

  5. 算法提高 道路和航路 SPFA 算法

    我简单的描述一下题目,题目中所说的有道路和航路: 1.公路是双向的,航路是单向的: 2.公路是正值,航路可正可负: 每一条公路i或者航路i表示成连接城镇Ai(1<=A_i<=T)和Bi(1 ...

  6. VS2015许可证过期

    VS2015过期激活方法

  7. 微信小程序,时间戳转为日期格式

    //数据转化 function formatNumber(n) { n = n.toString() ] ? n : ' + n } /** * 时间戳转化为年 月 日 时 分 秒 * number: ...

  8. 如何 “解决” WPF中空域问题(Airspace issuse)

    空域问题是由于Winform与WPF在底层渲染机制上有所区别而导致的.多数情况下,开发者为了实现不规则的窗体并承载Winform控件时,遇到此类问题.当WPF窗体设置为允许透明(也就是AllowsTr ...

  9. 小程序WXML 使用小结

    数据绑定 <view> {{message}} </view> // page.js Page({ data: { message: 'Hello MINA!' } }) 组件 ...

  10. 用JavaCV改写“100行代码实现最简单的基于FFMPEG+SDL的视频播放器 ”

    FFMPEG的文档少,JavaCV的文档就更少了.从网上找到这篇100行代码实现最简单的基于FFMPEG+SDL的视频播放器.地址是http://blog.csdn.net/leixiaohua102 ...