Problem K. Expression in Memories

本文介绍了一个有趣的问题:根据部分已知字符和问号组成的字符串,尝试还原一个有效的数学表达式。文章提供了一段代码实现,该算法通过替换问号来构造合法的数学表达式,并考虑了多种边界情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem K. Expression in Memories

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

代码:

#include <bits/stdc++.h>
using namespace std;
char s[505];
int main(){
    int t,n,f;
    scanf("%d",&t);
    getchar();
    while(t--){
        memset(s,0,sizeof(s));
        f=0;
        gets(s);
        n=strlen(s);
        for(int i=1;i<n;i++){
            if((s[i]=='+'||s[i]=='*')&&((s[i-1]=='+'||s[i-1]=='*'))){f=1;break;}
        }
        if(s[0]=='+'||s[0]=='*'||s[n-1]=='+'||s[n-1]=='*')f=1;
        for(int i=0;i<n-1;i++){
            if((s[i]=='0')&&(i==0||(s[i-1]=='+'||s[i-1]=='*'))){
                if(s[i+1]!='+'&&s[i+1]!='*'&&s[i+1]!='?'){f=1;break;}
                if(s[i+1]=='?'){
                    if(i+1==n-1||(s[i+2]=='+'||s[i+2]=='*')){f=1;break;}
                    s[i+1]='+';
                    if(s[i+2]=='?')s[i+2]='1';
                }
            }
            if((s[i]=='?')&&(i==0||(s[i-1]=='+'||s[i-1]=='*')))s[i]='1';
            else if((s[i]=='?')&&((s[i-1]!='+'||s[i-1]!='*'))){
                s[i]='1';
            }
        }
        if(f){
            puts("IMPOSSIBLE");continue;
        }
        for(int i=0;i<n;i++)if(s[i]=='?')s[i]='1';
        cout<<s<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值