模拟题----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): 1111 Accepted Submission(s): 432
Special Judge

Problem Description
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

利用操作符分割,用judge函数判断是否合法

#include
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const string sample = "+*";
const int N = 505;
char s[N];
bool judge(int l,int r)
{
    if(l > r){
        return false;
    }
    if(s[l] == '0'){
        if(l == r){
            return true;
        }
        else{
            if(s[l + 1] == '?'){
                s[l + 1] = '+';
                if(judge(l + 2,r)){
                    return true;
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }
    }
    else{
        for(int i = l;i <= r;++i)
        {
            if(s[i] == '?'){
                s[i] = '1';
            }
        }
    }
    return true;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        if(s[0] == '+' || s[0] == '*'){
            printf("IMPOSSIBLE\n");
            continue;
        }
        int pre = 0;
        bool flag = false;
        bool tmp = false;
        int len = strlen(s);
        for(int i = 0;i < len;++i)
        {
            if(i == len - 1){
                if(s[i] == '+' || s[i] == '*'){
                    flag = true;
                    break;
                }
                else{
                    if(tmp){
                        if(judge(pre,len - 1)){
                            continue;
                        }
                        else{
                            flag = true;
                            break;
                        }
                    }
                }
            }
            else{
                if(s[i] == '+' || s[i] == '*'){
                    tmp = true;
                    if(judge(pre,i - 1)){
                        pre = i + 1;
                        continue;
                    }
                    else{
                        flag = true;
                        break;
                    }
                }
            }
        }
        if(!tmp){
             if(!judge(0,len - 1)){
                printf("IMPOSSIBLE\n");
                continue;
             }
        }
        if(flag){
            printf("IMPOSSIBLE\n");
        }
        else{
            printf("%s\n",s);
        }
    }
    return 0;
}

你可能感兴趣的:(ACM__模拟)