2018 Multi-University Training Contest 4 K(Expression in Memories)

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

题意:给出一串字符,把“?”填上“*”或“+”或“1”或“0”,要求不能出现前导0,比如01,023,+098,00等。如果不行就输出IMPOSSIBLE

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

队友代码:

const int maxn=200010;
int n,m,k,kk,ans;
char s[maxn];
int main()
{
    int T;
    int f=1;
    scanf("%d",&n);{
        while(n--){
        scanf("%s",s);
            f=1;k=-2;kk=-1;
            int l=strlen(s);
            for(int j=0;j<l;j++)
            {
                if(s[j]=='0'&&kk<k) continue;
                if(s[j]=='0')
                {
                    ans=0;
                    if(j+1<l&&s[j+1]=='?') {ans=1;s[j+1]='+';}
                    if(j+1<l&&j-1>=0&&(s[j-1]=='+'||s[j-1]=='*')&&(s[j+1]=='+'||s[j+1]=='*')) {ans=1;}
                    if(j+1>=l&&j-1>=0&&(s[j-1]=='+'||s[j-1]=='*')) {ans=1;}
                    if(j+1<l&&j-1<0&&(s[j+1]=='+'||s[j+1]=='*')){ans=1;}
                    if(!ans){f=0;break;}
                }
                if(s[j]=='0')
                {
                    if(j==0&&j+1<l&&(s[j+1]=='*'||s[j+1]=='+')){ans=1;}
                    else if(j==l-1&&j-1>=0&&(s[j-1]=='*'||s[j-1]=='+')){ans=1;}
                    else if(j+1<l&&j-1>=0&&(s[j-1]=='+'||s[j-1]=='*')&&(s[j+1]=='+'||s[j+1]=='*')){ans=1;}
                    else if(kk>k) {f=0;break;}
                }
                if(j==0&&(s[j]=='*'||s[j]=='+')) f=0;
                if(j==l-1&&(s[j]=='*'||s[j]=='+')) f=0;
                if(j+1<l&&(s[j]=='+'||s[j]=='*')&&(s[j+1]=='+'||s[j+1]=='*')) f=0;
                if(s[j]=='?') {s[j]='1';}
                if(s[j]>='1'&&s[j]<='9')k=j;
                if(s[j]=='*'||s[j]=='+')kk=j;
                if(!f) break;
            }
            if(!f) puts("IMPOSSIBLE");
            else printf("%s\n",s);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/snayf/article/details/81394252