题目链接: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. 通过id、classname定位元素,程序仍报找不到元素的原因

    很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug也可以看到,就是定位不到,这种情况很有可能是frame在搞鬼.我们知道了原因,你现在就解决问题.sw ...

  2. python3 continue和break 区别

    for i in range(10): if i==5: continue #跳出当次循环 if i==8: break #跳出整个for循环 print(i)

  3. 简述wcf应用

    一.新建wcf 如下图:wcf可以简历俩种形式 1.库文件,就是一个类库文件,可以用windows服务或控制台开启. 2.服务应用程序,可以直接IIS上面发布. 二.库文件自动生成的类 接口类 usi ...

  4. spring @Transactional事务失效

    不开事务几种情形 ① @Transactional写在了private方法上 org.springframework.transaction.interceptor.AbstractFallbackT ...

  5. Spyder清除Variable Explorer&&手动安装protobuf3.0(为了配置windows的python接口)

    输入:reset 选择:y PS:建议在windows下,安装anaconda32bit版本的,可以兼容更多第三方包.   Conda使用清华镜像 配置镜像 在conda安装好之后,默认的镜像是官方的 ...

  6. c#之GDI简单实现代码及其实例

    作业:文档形式 3到5页理解 1.理解 2.源代码解释(1到2页) 3.实现效果 项目地址: https://github.com/zhiyishou/polyer Demo:https://zhiy ...

  7. Linux 进程间通信之管道(pipe),(fifo)

     无名管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信: 定义函数: int pipe(int f ...

  8. Android NDK开发 Jni中打日志LOG(二)

    HelloJni.c文件中,加入头文件和函数声明.最终文件如下: #include <jni.h> #include <string.h> #include<androi ...

  9. Murano Weekly Meeting 2015.09.15

    Meeting time: 2015.September.15th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting s ...

  10. 使用require.js

    requirejs使用入门 什么是requirejs? RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签的脚本加载步骤.可以用它来加速.优化代码,但其主要 ...