Problem K. Expression in Memories

Problem K. Expression in Memories

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
Special Judge

 

Problem Description

Kazari remembered that she had an expression s0 before.
Definition of expression is given below in Backus–Naur form.
<expression> ::= <number> | <expression> <operator> <number>
<operator> ::= "+" | "*"
<number> ::= "0" | <non-zero-digit> <digits>
<digits> ::= "" | <digits> <digit>
<digit> ::= "0" | <non-zero-digit>
<non-zero-digit> ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
For example, `1*1+1`, `0+8+17` are valid expressions, while +1+1, +1*+1, 01+001 are not.
Though s0 has been lost in the past few years, it is still in her memories. 
She remembers several corresponding characters while others are represented as question marks.
Could you help Kazari to find a possible valid expression s0 according to her memories, represented as s, by replacing each question mark in s with a character in 0123456789+* ?

 

Input

The first line of the input contains an integer T denoting the number of test cases.
Each test case consists of one line with a string s (1≤|s|≤500,∑|s|≤105).
It is guaranteed that each character of s will be in 0123456789+*? .

 

Output

For each test case, print a string s0 representing a possible valid expression.
If there are multiple answers, print any of them.
If it is impossible to find such an expression, print IMPOSSIBLE.

 

Sample Input

 

5

?????

0+0+0

?+*??

?0+?0

?0+0?

 

Sample Output

 

11111

0+0+0

IMPOSSIBLE

10+10

IMPOSSIBLE

 

wa的我心慌,对了,

isdigit(x),x为char类型,如果x>='0'&&x<='9'返回值为true(1)否则返回false(0)

是判断一个字符是否是数字的函数

填空

1      0?    ? = +      (0是第一个)

2      +0?或者*0?    ? = +

3      ?             ? = 1

查询

1     **,++,*+,+*不行

2    01不行

3    +--------------+       最前,后不能是+或*

#include<bits/stdc++.h>
#define ll long long
using namespace std;
char a[100005];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",a);
        int len = strlen(a);
        for(int i=0;i<len;i++)
        {
            if(a[i]=='?')
            {
                if(i==0)
                    a[i]='1';
                else
                {
                    if(i-2>=0 && !isdigit(a[i-2]) && a[i-1]=='0')
                        a[i]='+';
                    else if(i==1 && a[0]=='0')
                        a[i]='+';
                    else
                        a[i]='1';
                }
            }
        }
        int flag = 0;

        for(int i=0;i<len-1;i++)
        {
            if(!isdigit(a[i])&&!isdigit(a[i+1]))
            {
                flag = 1;
                break;
            }
            else if((i==0 || !isdigit(a[i-1]))&& a[i]=='0' && isdigit(a[i+1]))
            {
                flag = 1;
                break;
            }
        }
        if(!isdigit(a[0])||!isdigit(a[len-1]))
            flag = 1;
        if(flag)printf("IMPOSSIBLE\n");
        else printf("%s\n",a);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/81347616