题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6342

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

题意:给你n个字符串,其中?可以变成数字、+和*,要尽可能让其符合规则,如果不能输出“IMPOSSIBLE”,有多种符合的情况,输出一种即可。

规则:

1、0不可以放在数字0到9的前面;

2、运算符号不可以连续出现。

AC代码:

#include<iostream>
#include<cstring>
using namespace std;
char m[100005];
int main()
{
    int a,b,c,d,e,f,g;
    cin>>a;
    while(a--)
    {
        cin>>m;
        b=strlen(m);
        d=0;
        for(c=0;c<b-1;c++)
        {
            if(m[c]=='0')
            {
                if(m[c+1]=='?')
                    m[c+1]='+';
                else if(m[c+1]>='0'&&m[c+1]<='9')
                {
                    d=1;
                    break;
                }
            }
            else if(m[c]>='1'&&m[c]<='9'||m[c]=='?')
            {
                while(1)
                {
                    c++;
                    if(m[c]=='+'||m[c]=='*'||c>=b-1)
                        break;
                }
            }
        }
        for(c=0;c<b;c++)
        {
            if(m[c]=='?')
                m[c]='1';
        }
        if(m[0]=='+'||m[0]=='*'||m[b-1]=='+'||m[b-1]=='*')
        {
            cout<<"IMPOSSIBLE"<<endl;
            continue;
        }
        if(b==1&&m[0]=='0')
        {
            cout<<m<<endl;
            continue;
        }
        for(c=0;c<b-1;c++)
        {
            if(m[c]=='+'||m[c]=='*')
            {
                if(m[c+1]=='+'||m[c+1]=='*')
                {
                    d=1;
                    break;
                }
            }
        }
        if(d==1)
            cout<<"IMPOSSIBLE"<<endl;
        else
            cout<<m<<endl;
    }
    return 0;
}