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.
::= |
::= “+” | “*”
::= “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
题意:将问号填充为数字或者’+’或’ב,符号隔开的数字不能有前导0.
AC代码:
#include<cctype>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e5+5;
char s[N];
int flag[N],n;
bool issymbol(char c)
{
if(c=='+'||c=='*')
return 1;
return 0;
}
bool is_ok()
{
int len = strlen(s);
memset(flag,0,sizeof(flag));
for(int i=0;i<len;i++)
{
if(s[i]=='?')
{
flag[i] = 1;
s[i] = '1';
}
}
for(int i=0;i<len;i++)
{
if(s[i]=='0'&&(i==0||(i>0&&issymbol(s[i-1])))&&i<len-1&&isdigit(s[i+1]))
{
if(flag[i+1]==1)
s[i+1] = '+';
else
return false;
}
}
if(issymbol(s[0])||issymbol(s[len-1]))
return false;
for(int i=0;i<len-1;i++)
if(issymbol(s[i])&&issymbol(s[i+1]))
return false;
return true;
}
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int t = is_ok();
if(t==1)
puts(s);
else
printf("IMPOSSIBLE\n");
}
return 0;
}