HDU6342 Problem K. Expression in Memories

 

原题:http://acm.hdu.edu.cn/showproblem.php?pid=6342

Problem K. Expression in Memories

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

分析:题目意思很容易理解,把题目给的表达式变成合法的表达式并输出,如果没有办法修改成合法的表达式就输出IMPOSSIBLE。很容易的分析出单出现0?,(+|*)0?的时候就需要把?修改成(*|+)即可,其余的情况把?改成1就行了,然后在判断修改后的表达式合不合法即可。这道在做的时候一开始的做法是先判断能不能修改合法的表达式然后在输出,但是不知道wa的好几发,到现在也不知道哪里错了,wa了几发以后感觉可能是算法有问题,于是就改成先修改,然后一边判断是否是合法的,若不合法就直接跳出,然后就直接过,不知道为啥一开始的代码哪里有问题(QAQ)。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000+100;
char s[maxn];
int t, n;

int main(){
    scanf("%d", &t);
    while(t--){
        memset(s, 0, sizeof(s));
        int flag = 0;
        scanf("%s", s);
        int len = strlen(s);
        for(int i=0; i<len; i++){
            if(s[i] == '+' || s[i] == '*' ){
                if(i == 0 || i == len-1){
                    flag = 1;
                    break;
                }
                if(s[i+1] == '+' || s[i+1] == '*'){
                    flag = 1;
                    break;
                }
            }
            else if(s[i] == '0'){
                if(s[i-1]=='+' || s[i-1]=='*' || i==0){
                    if(s[i+1] >= '0' && s[i+1] <= '9'){
                        flag = 1;
                        break;
                    }
                    else if(s[i+1] == '?') s[i+1] = '+';
                }
                else {
                    if(s[i+1] == '?') s[i+1] = '1';
                }
            }
            else if(s[i] == '?') s[i] = '1';
        }
        if(flag == 1){
            printf("IMPOSSIBLE\n");
            continue;
        }
        else{
            printf("%s\n", s);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37611893/article/details/81348920
今日推荐