HDU6342 Problem K. Expression in Memories(2018HDU多校联赛第四场,模拟)

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

思路

给出一个字符串,让你判断表达式是否合法,关于是否合法的定义看一看题面就知道了。

我们首先判断:

  1. 当当前位置为数字的时候:

    • 当前数字为0:前一个位置为符号时,后面一个一定不能出现数字,否则含有前导0,当后面一个是问号,则只可以填一个符号,用@来代替
    • 不为0的时候,如果后面一个是问号,则填一个数字,用#代替
  2. 当当前位置为符号的时候:

    • 如果前后位置出现符号,则一定不符合
    • 如果后一个位置是问号,则填写一个数字
  3. 当当前位置为问号的时候,填写一个数字

判断完成后,直接输出结果,@替换成+号,#替换乘数字1,这题有一个坑点,就是给出的串中可能包含空格,注意处理

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int N = 1e5 + 10;
char str[N], s[N];
int main()
{
    // freopen("in.txt", "r", stdin);
    int t;
    scanf("%d%*c", &t);
    while (t--)
    {
        mem(s, '\0');
        mem(str, '\0');
        vector<int> v;
        int tot = 1;
        gets(str);
        int str_len = strlen(str);
        for (int i = 0; i < str_len; i++)
            if (str[i] != ' ')
                s[tot++] = str[i];
            else
                v.push_back(i);
        s[0] = '+';
        s[tot] = '+';
        int n = tot;
        int flag = 1;
        for (int i = 1; i <= n; i++)
        {
            if (isdigit(s[i]))
            {
                if (s[i] == '0')
                {
                    if (s[i - 1] == '+' || s[i - 1] == '*' || s[i - 1] == '@')
                    {
                        if (isdigit(s[i + 1]))
                            flag = 0;
                        if (s[i + 1] == '?')
                            s[i + 1] = '@';
                    }
                }
                else if (s[i + 1] == '?')
                    s[i + 1] = '#';
            }
            if (s[i] == '+' || s[i] == '*')
            {
                if (s[i - 1] == '+' || s[i - 1] == '*' || s[i - 1] == '@')
                    flag = 0;
                if (s[i + 1] == '+' || s[i + 1] == '*')
                    flag = 0;
                if (s[i + 1] == '?')
                    s[i + 1] = '#';
            }
            if (s[i] == '?')
                s[i] = '#';
            if (!flag)
                break;
        }
        if (flag)
        {
            for (int i = 0, j = 1; i < str_len; i++)
            {
                if (str[i] == ' ')
                    printf(" ");
                else
                {
                    if (s[j] == '#')
                        printf("1");
                    else if (s[j] == '@')
                        printf("+");
                    else
                        printf("%c", s[j]);
                    j++;
                }
            }
            puts("");
        }
        else
            puts("IMPOSSIBLE");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/riba2534/article/details/81382274