Expression in Memories Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem Description Kazari remembered that she had an expression s0 before.
Input The first line of the input contains an integer T denoting the number of test cases.
Output For each test case, print a string s0 representing a possible valid expression.
Sample Input
5 ????? 0+0+0 ?+*?? ?0+?0 ?0+0?
Sample Output
11111 0+0+0 IMPOSSIBLE 10+10 IMPOSSIBLE
|
思路:+0?这种情况?一定是+或者*,其他情况的?是非0的数字
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100010;
char s[maxn];
int len;
int judge()
{
int i;
if(s[0]=='*'||s[0]=='+')
return 0;
if(s[len-1]=='*'||s[len-1]=='+')
return 0;
for(i=0;i<len;i++)
{
if(s[i]=='*'||s[i]=='+')
{
if(s[i+1]=='*'||s[i+1]=='+')
return 0;
}
if(s[i]=='0')
{
if(s[i-1]=='*'||s[i-1]=='+')
{
if(s[i+1]>='0'&&s[i+1]<='9')
return 0;
}
}
if(i==0)
{
if(s[i]=='0')
{
if(s[i+1]>='0'&&s[i+1]<='9')
return 0;
}
}
}
return 1;
}
int main()
{
int n;
scanf("%d",&n);
getchar();
//101010 01010 +0?
while(n--){
memset(s,0,sizeof(s));
scanf("%s",&s);
len = strlen(s);
if(s[0]=='0'){ //先判断第一个是否是0后面是否是?,这算是一种特殊情况了
if(s[1]=='?')
s[1]='+';
}
for(int i=1;i<len-1;i++){
if(s[i]=='0'){
if(s[i-1]=='+' || s[i-1]=='*'){
if(s[i+1]=='?')
s[i+1]='+';
}
}
}
for(int i=0;i<len;i++){
if(s[i]=='?')
s[i]='1';
}
//puts(s);
int ans = judge();
if(ans==0){
printf("IMPOSSIBLE\n");
}
else{
puts(s);
}
}
return 0;
}