HDU 6342(模拟)

传送门:

题面:

    

Problem K. Expression in Memories

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 160    Accepted Submission(s): 65
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

题目描述:

    给你一个由0123456789+*? 这些字符的字符串,其中问号'?'可以变为任意字符,问你是否可以构成一个正常的多项式(无前导0)。

题目分析:

    在题目中有一个坑点,就是在类似 +0? 的情况下, ? 须被替换为 + 或 * ,其余情况直接将 ? 替换为非零数字就好。而对于某一位,一共就只有前面为0,为其他数字,为+/*,为开头四种情况,因此我们只需要用一个标记f记录某一位的状态是什么,之后进行分类讨论即可。具体细节请看代码。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=507;
char str[maxn];
int main(){
    int t;
    cin>>t;
    while (t--){
        scanf("%s",str);
        int f=0;
        //用来记录不同的可能,f==0代表开头,f==-1代表不存在,f==1代表为数字,f==2代表+/*,f==3代表0
        int len=strlen(str);
        for (int i=0;i<len;i++){
            if (str[i]=='?') {
                if (f==1||f==2||f==0) {//如果之前为数字或为+/* 或目前为开头且当前为?则可以填数字1
                    str[i]='1';
                    f=1;
                }
                else if (f==3) {//如果之前为0,而现在是? (对应+0?这种情况)
                    str[i]='+';
                    f=2;
                }
            }
            else if (str[i]=='+'||str[i]=='*'){//如果现在是符号位
                if (f==0||f==2) {//如果当前为开头或之前为符号位,则退出
                    f=-1;
                    break;
                }
                else f=2;
            }
            else if (str[i]=='0'){//如果当前为0
                if (f==3){//如果之前为开头,则退出
                    f=-1;
                    break;
                }
                else if (f!=1) f=3;//如果不是开头且之前不为数字,则此位可填0
            }
            else {//如果为其他数字
                if (f==3){//如果前一位为0,则退出
                    f=-1;
                    break;
                }
                else f=1;
            }
        }//如果之前填的是数字
        if (f==1||f==3){
            puts(str);
        }//否则不存在
        else puts("IMPOSSIBLE");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Chen-Jr/p/11007252.html