杭电多校第四场 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): 0    Accepted Submission(s): 0
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

先考虑运算符不行的情况再考虑前导0的情况 

AC代码
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
string s;
vector<string> e;
bool iso( char c ) {
    if( c == '+' || c == '*' ) {
        return true;
    }
    return false;
}
int main() {
    ll T;
    cin >> T;
    while( T -- ) {
        cin >> s;
        bool flag = true;
        for( ll i = 0; i < s.length(); i ++ ) {
            if( ( i == 0 || i == s.length()-1 ) && iso(s[i]) ) {
                flag = false;
                break;
            }
            if( i < s.length()-1 ) {
                if( iso(s[i]) && iso(s[i+1]) ) {
                    flag = false;
                    break;
                }
            }
            if( s[i] == '?' ) {
                if( s[i-1] == '0' && ( i-2 < 0 || iso(s[i-2])  ) && !iso(s[i+1]) ) {
                    s[i] = '+';
                } else {
                    s[i] = '1';
                }
            }
        }
        //debug(s);
        e.clear();
        string t = "";
        for( ll i = 0; i < s.length(); i ++ ) {
            if( iso(s[i]) || i == s.length()-1 ) {
                if( i == s.length()-1 ) {
                    t += s[i];
                }
                e.push_back(t);
                t = "";
            } else {
                t += s[i];
            }
        }
        for( ll i = 0; i < e.size(); i ++ ) {
            //cout << e[i] << endl;
            if( e[i][0] == '0' && e[i].length() > 1 ) {
                flag = false;
                break;
            }
        }
        if( flag ) {
            cout << s << endl;
        } else {
            cout << "IMPOSSIBLE" << endl;
        }
    }
    return 0 ;
}

  

猜你喜欢

转载自www.cnblogs.com/l609929321/p/9402017.html