hdu Problem K. Expression in Memories

              Problem K. Expression in Memories

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 262144/262144K (Java/Other)

Total Submission(s) : 1   Accepted Submission(s) : 1

Special Judge

Problem Description

Kazari remembered that she had an expression $s_0$ 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 $s_0$ 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 $s_0$ 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 \le |s| \le 500, \sum {|s|} \le 10 ^ 5)$. It is guaranteed that each character of $s$ will be in 0123456789+*? .

 

 

Output

For each test case, print a string $s_0$ 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

 

 

Source

2018 Multi-University Training Contest 4

思路:規則大概概括爲:不能出現前導零,符號兩邊必須是合法數字。我們先把所有問號改好,再去判斷現在是否合法,否則問題會變得很比不分開判斷複雜。比如0?0判斷到第2個0時你還要去看前一個?是什麼。

下面的講解問號只改爲+或1...

對於(null)0?,+0?,*0?一律只能改爲+,否則必是前導零,其他情況問號改爲1,判斷情況的時候注意一下i的範圍

首位和最後一位不能是+/*,

1+0?,01,++/+*/**/*+,這些情況都不對

#include<bits/stdc++.h>
using namespace std;
char a[101000];
int len;
int pd()
{
    int i;
    if(a[0]=='+'||a[0]=='*')
        return 0;
    if(a[len-1]=='+'||a[len-1]=='*')
     return 0;
    for(i=0;i<len;i++)
    {
        if(a[i]=='+'||a[i]=='*')
        {
                if(a[i+1]=='+'||a[i+1]=='*')
                {
                    return 0;
                }
        }
        if(a[i]=='0')
        {
            if(a[i-1]=='+'||a[i-1]=='*')
            {
                if(a[i+1]>='0'&&a[i+1]<='9')
                {
                    return 0;
                }
            }
        }
        if(i==0)
        {
            if(a[i]=='0')
            {
                if(a[i+1]>='0'&&a[i+1]<='9')
                {
                    return 0;
                }
            }
        }
    }
    return 1;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(a,0,sizeof(a));
        scanf("%s",a);
        len= strlen (a);
        if(a[0]=='0')
        {
            if(a[1]=='?')
            {
                a[1]='+';
            }
        }
        for(int i=1;i<len-1;i++)
        {
            if(a[i]=='0')
            {
                if(a[i-1]=='+'||a[i-1]=='*')
                {
                    if(a[i+1]=='?')
                    {
                        a[i+1]='+';
                    }
                }
            }
        }
        for(int i=0;i<len;i++)
        {
            if(a[i]=='?')
            {
                a[i]='1';
            }
        }
        int k = pd();
        if(k==0)
        {
            printf("IMPOSSIBLE\n");
        }
        else
        {
            puts(a);
        }

    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章