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. 最实用、最常用的jQuery代码片段

    // chinacoder.cn JavaScript Document $(document).ready(function() { //.filter(":not(:has(.selec ...

  2. CRC校验和网络通信中writen、readn函数

    1.对网络传输的数据进行CRC32校验. #include <stdint.h> #include <stdio.h> #include <string.h> st ...

  3. C#笔试面试宝典值得收藏1

    技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...

  4. 用Jquery控制文本框只能输入数字和字母及jquery自定义方法$.fn

    封装成onlyNum(),onlyAlpha()和onlyNumAlpha()3个Jquery扩展方法,方便复用,由于里面一些JS代码涉及到了"禁用输入法,获取剪切板的内容",而& ...

  5. MVVMLight

    MVVMLight源码分析之消息机制和ViewModelBase http://www.cnblogs.com/facingwaller/archive/2010/11/06/1870608.html ...

  6. 为每个页面加上Session判断

    首先新建一个类,继承自System.Web.UI.Page,然后重写OnInit,如下: using System; using System.Data; using System.Configura ...

  7. COOKIE和session的机制详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  8. js 阻止事件冒泡 支持所有主流浏览器

    function getEvent(){ if(window.event) {return window.event;} func=getEvent.caller; while(func!=null) ...

  9. log4j2配合slf4j使用

    说明 log4j2被用于日志输出,相信绝大多数程序猿都对此不陌生.笔者刚接触log4j2,因此记个博客备用. log4j2是一个日志框架,slf4j是日志框架接口,之所以使用log4j2和slf4j搭 ...

  10. oracle9i 精简版客户端界面没有显示实例名

    1.右击"我的电脑",选择"属性"菜单项 2.在弹出的窗口中,点击"高级系统设置" 3.在弹出的窗口中,选择"环境变量" ...