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. mybatis想要在控制台显示sql语句配置文件

    在src目录下创建一个properties文件 配置内容如下 log4j.rootLogger=debug,stdout,logfile log4j.appender.stdout=org.apach ...

  2. oblique perspective projection

    参考: https://en.wikibooks.org/wiki/GLSL_Programming/Vertex_Transformations <3D游戏与计算机图形学中的数学方法>E ...

  3. shared-service.ts

    shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...

  4. Windows 2003 远程桌面

  5. Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException

    Atitit.故障排除系列---NoClassDefFoundError  NoClassDefFoundError ClassNotFoundException 1. java.lang.Class ...

  6. lua 打印 table 拷贝table

    -- 打印table function print_lua_table (lua_table, indent) if lua_table == nil or type(lua_table) ~= &q ...

  7. 通过ip查看主机名和端口占用情况

      1. 知道对方ip查看对方的计算机名 方法:开始->运行->cmd->net view 对方ip 或者 开始->运行->cmd->nbtstat -a 对方ip ...

  8. IBATIS中‘$’与‘#’使用

    IBATIS中关于iterate和‘$’与‘#’的应用 一个包含List元素的HashMap参数赋给sqlMap  public int getCountById(String id, String ...

  9. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  10. Linux下vi命令小结

     进入vi的命令         vi filename :打开或新建文件,并将光标置于第一行首    vi n filename :打开文件,并将光标置于第n行首    vi filename :打 ...