Problem K. Expression in Memories
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 302 Accepted Submission(s): 112
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
Source
2018 Multi-University Training Contest 4
Recommend
chendu | We have carefully selected several similar problems for you: 6343 6342 6341 6340 6339
Statistic | Submit | Discuss | Note
Home | Top | Hangzhou Dianzi University Online Judge 3.0 Copyright © 2005-2018 HDU ACM Team. All Rights Reserved. Designer & Developer : Wang Rongtao LinLe GaoJie GanLu Total 0.000000(s) query 6, Server time : 2018-08-01 20:31:08, Gzip enabled | Administration |
题意:给你一个表达式的定义:
1.每个数字不能有前导0;
2.+*不能连在一起;
3.+和*不能在表达式的头和尾;
4.表达式由“0123456789+*?”字符组成;
现在因为Kazari忘记了之前的表达式,但是还是知道一些位置的字符,其他不知道的用“?”代替。问是否可以知道用“0123456789+*”其中的字符替换“?”成为一个合格的表达式。若符合,输出其中一种,不符合,输出IMPOSSIBLE
解析:模拟题,注意0?0,120?0,120?1这几种情况。
#include<bits/stdc++.h>
using namespace std;
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int main()
{
int T;scanf("%d",&T);
while(T--)
{
char s[1050];
scanf("%s",s);
int len=strlen(s);
s[len]='+';
int flag=0;
for(int i=0;i<len;i++)
{
if(s[i]=='?')
{
if(i==0) s[i]='1';
else if(i==1)
{
if(s[i-1]=='0') s[i]='+';
else s[i]='1';
}
else
{
if((s[i-1]=='0'&&isdigit(s[i-2]))||s[i-1]!='0') s[i]='1';
else s[i]='+';
}
}
}
for(int i=0;i<len;i++)
{
if(i==0)
{
if(s[i]=='0') if(isdigit(s[i+1])) flag=1;
if(s[i]=='+'||s[i]=='*') flag=1;
}
else
{
if((s[i]=='+'||s[i]=='*')&&(s[i+1]=='+'||s[i+1]=='*')) flag=1;
if(s[i]=='0'&&(s[i-1]=='+'||s[i-1]=='*')&&isdigit(s[i+1])) flag=1;
}
}
if(flag)puts("IMPOSSIBLE");
else
{
for(int i=0; i<len; i++)
{
printf("%c",s[i]);
}
puts("");
}
}
return 0;
}