Problem K. Expression in Memories HDU - 6342(模拟)

Problem K. Expression in Memories HDU - 6342

Kazari remembered that she had an expression s0 before.
Definition of expression is given below in Backus–Naur form.
::= |
::= “+” | “*”
::= “0” |
::= “” |
::= “0” |
::= “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

题意就是让你在?里添加上合适的1或者+,并判断该式子是否合法

注意在类似 +0? 的情况下, ? 须被替换为 + 或 * ,其余情况直接将 ? 替换为非零数字就好。替换完成后判断一下是否合法。

模拟题一开始两人写越写越乱,最后队友一人写出来过了

code:

#include 
using namespace std;
char ch[510];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int flag = 0;
        scanf("%s",ch);
        int len = strlen(ch);
        for(int i = 0; i < len; i++){
            if(i > 1 && (ch[i-2] == '+' || ch[i-2] == '*') && ch[i-1] == '0' && ch[i] == '?')
                ch[i] = '+';
            if(i == 1 && ch[i] == '?' && ch[i-1] == '0')
                ch[i] = '+';
            else if(ch[i] == '?')
                ch[i] = '1';
        }
        if(ch[len-1] == '+' || ch[len-1] == '*' || ch[0] == '+' || ch[0] == '*')
            flag = 1;
        else{
            for(int i = 0; i < len; i++){
                if(i == 1 && ch[i-1] == '0' && ch[i] >= '0' && ch[i] <= '9')
                    flag = 1;
                else if(i > 1 && ch[i-1] == '0' && (ch[i-2] == '+' || ch[i-2] == '*') && ch[i] >= '0' && ch[i] <= '9')
                    flag = 1;
                else if((ch[i-1] == '+' || ch[i-1] == '*') && (ch[i] == '+' || ch[i] == '*'))
                    flag = 1;
            }
        }
        if(flag)
            printf("IMPOSSIBLE\n");
        else
            printf("%s\n",ch);

    }
    return 0;
}

你可能感兴趣的:(#,暴力,#,思维技巧)