题目链接:http://codeforces.com/contest/664/problem/B

B. 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

解题报告:

看到大神们的代码,简直碉堡了。

1、这里的输入要掌控好。

2、思路:先置每个位置为1,记录下加上的数的个数sa,减去的个数sb,这样,p(离目标结果)只差p-n了。

3、根据每个问号前面的符号,自增,达到结果。

#include <cstdio>

using namespace std;

int a[];    ///结果
int v[]; ///记录每个符号 char c[]; ///符号命令 int main()
{
scanf("%s",c); ///输入问号
scanf("%s",c); ///输入符号
int m=; ///?的个数,即要填的数字的个数
int sa=; ///加数的个数
int sb=; ///减数的个数
v[]=; while(c[]!='=')
{
if(c[]=='+')
{
v[++m]=;
sa++;
}
else {
v[++m]=-;
sb++;
}
scanf("%s",c);///输入问号
scanf("%s",c);///输入符号
} int n;
scanf("%d",&n); ///目标结果 for(int i=;i<=m;i++) ///全部填上1
a[i]=; int p=sa-sb; for(int i=;i<=m;i++) ///一个一个处理各个数
{
while((p<n)&&(v[i]==)&&(a[i]<n))
a[i]++,p++;
while((p>n)&&(v[i]==-)&&(a[i]<n))
a[i]++,p--;
} if(p!=n)
{
printf("Impossible\n");
return ;
}
printf("Possible\n");
printf("%d ",a[]); for(int i=;i<=m;i++)
{
if(v[i]==) printf("+ ");
else printf("- ");
printf("%d ",a[i]);
}
printf("= %d\n",n);
}

Codeforces Round #347 (Div.2)_B. Rebus的更多相关文章

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

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

  2. Codeforces Round #347 (Div. 2)

    unrating的一场CF A - Complicated GCD #include <bits/stdc++.h> const int N = 1e5 + 5; char a[105], ...

  3. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律

    题目链接: http://codeforces.com/contest/664/problem/C 题解: 这题最关键的规律在于一位的有1989-1998(9-8),两位的有1999-2098(99- ...

  4. Codeforces Round #347 (Div.2)_A. Complicated GCD

    题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...

  5. Codeforces Round #290 (Div. 2) _B找矩形环的三种写法

    http://codeforces.com/contest/510/status/B 题目大意 给一个n*m  找有没有相同字母连起来的矩形串 第一种并查集 瞎搞一下 第一次的时候把val开成字符串了 ...

  6. Codeforces Round #347 (Div. 2) (练习)

    A: 题意:找到[a, b]的最大公约数: 思路:相同时为本身,不同时为1. 套路:碰到水题别想太多: 猜想:两个相邻数,必有一奇一偶,如果偶数有因子3或者其他,奇数可不可能有相同的呢? 枚举一些数后 ...

  7. codeforces Round #347 (Div. 2) C - International Olympiad

    思路:从后往前一位一位的模拟,每次判断一下当前枚举的数是否之间枚举过了.或者当前枚举数过小,小于1989. #include<cstdio> #include<cstring> ...

  8. Codeforces Round #366 (Div. 2)_B. Spider Man

    B. Spider Man time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #332 (Div. 2)_B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

随机推荐

  1. RequireJS -Javascript模块化(一、简介)

    1.认识RequireJS RequireJs官网(http://requirejs.org/)的描述: RequireJS is a JavaScript file and module loade ...

  2. 转 python中%s与%d

    https://blog.csdn.net/SuperCreators/article/details/81393977 pythn print格式化输出. %r 用来做 debug 比较好,因为它会 ...

  3. 数据库mysql基础语言--各模式的含义

    1. 欢迎信息 欢迎来到 MySQL 镜像.命令以 ; 或 g 结束.你的 MySQL 连接编号为 2.服务器版本:5.1.47-社区 MySQL 社区服务器(GPL) 版权(C)2000.2010, ...

  4. [转]win系统下nodejs安装及环境配置

    本文转自:http://www.cnblogs.com/linjiqin/p/3765390.html 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  5. React.js 小书 Lesson3 - 前端组件化(二):优化 DOM 操作

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson3 转载请注明出处,保留原文链接和作者信息. 看看上一节我们的代码,仔细留意一下 change ...

  6. 【蓝牙】蓝牙,调试 hcitool与gatttool实例

    Bluez协议栈在安装完以后,会提供两个命令行调试工具,hcitool与gattool,我们可以根据提供的工具来轻松的调试我们的蓝牙设备,调试BLE设备时,需要获取root权限. 蓝牙设备的开启与关闭 ...

  7. (初学)wpf仿QQ界面-整体布局

    跟一个小学弟一起学习wpf,小学弟是刚初中毕业,对编程刚刚接触,我挺怕自己带的不好,影响小学弟以后在编程方向的学习兴趣.我承认自己水平不高,但是在努力去学习新知识!一起加油吧!在此以博客,记录学习进度 ...

  8. Dubbo分布式日志追踪

    使用dubbo分布式框架进行微服务的开发,一个大系统往往会被拆分成很多不同的子系统,并且子系统还会部署多台机器,当其中一个系统出问题了,查看日志十分麻烦. 所以需要一个固定的流程ID和机器ip地址等来 ...

  9. Redis整理第三波(生存时间、事务管理)

    expire  设置生存时间 Redis在实际使用过程中更多的用作缓存,然而缓存的数据一般都是需要设置生存时间的,即到期后数据销毁. TTL查看key的剩余时间,当返回值为-2时,表示键被删除. 当 ...

  10. html meta标签实现页面跳转

    refresh用于刷新与跳转(重定向)页面 refresh出现在http-equiv属性中,使用content属性表示刷新或跳转的开始时间与跳转的网址 <!DOCTYPE html> &l ...