• CodeForces 663A Rebus


    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
    ");
            return 0;
        }
        printf("Possible
    ");
        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;
    }


    
    

  • 相关阅读:
    校软第二次考核11.11
    计算机视觉入门live笔记
    大数阶乘题解
    校软招新考试 原卷 题解
    校软件班招新考试A卷 题解
    【好题】最短路变形+dp——ICPC NCNA 2018G
    贪心/二分——cf1342D
    思维+贪心+dp——cf1341D
    【经典】容斥+排列组合——cf1342E
    计算几何+求质心+求多边形与圆交面积——ICPC GNYR 2019
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228678.html
Copyright © 2020-2023  润新知