HDU 杭电多校2018 1011 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

 

Statistic | Submit | Clarifications | Back

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
#define maxn 100005
#define eps 0.0000001
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴栈用的
/*
题目大意:给定一个表达式里面参杂有问号,
问好可填(。。。),问是否能填补成正确的表达式。
如果能,则输出补全的即可。

首先递归,递归的条件是对+,*进行拆分。
最后得到的是不参运算符的表达式。

对这个表达式判定,
如果第一个字符不为0,则一定是符合条件的(补全1即可)。
如果是0,如果单个0可以,不是的话继续判,
如果下一位是数字直接判错,如果不是,递归处理,补全加号即可。
*/
bool judge(char c)
{
    if(c=='+'||c=='*') return true;
    return false;
}

char seq[maxn];

int chuan(int l,int r)
{
    if(l>r) return 0;
    if(l==r) return 1;
    if(seq[l]!='0') return 1;
    if(seq[l+1]!='?') return 0;
    seq[l+1]='+';
    return chuan(l+2,r);
}

int Dfs(int l,int r)
{
    if(l>r) return false;
    int flag1,flag2;
    for(int i=l;i<=r;i++)
    {
        if(judge(seq[i]))
        {
            flag1=Dfs(l,i-1);
            flag2=Dfs(i+1,r);
            return flag1&flag2;
        }
    }
    return chuan(l,r);
}

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%s",seq);
        int len=strlen(seq);
        if(Dfs(0,len-1))
        {
            for(int i=0;i<len;i++)
            {
                if(seq[i]=='?') seq[i]='1';
            }
            printf("%s\n",seq);
        }
        else puts("IMPOSSIBLE");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81333920
今日推荐