HDU 6342 Problem K. Expression in Memories (2018 Multi-University Training Contest 4)

Problem K. Expression in Memories

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 733    Accepted Submission(s): 281
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

题目大意:

输入一个t,然后跟着t行数据,假如某行的数据里含有‘?’,‘?’可以替换0~9或者‘*’,‘+’。也就是可以变换为任意字符(符合题意的)。然后判断每一行的数据是否为一个表达式,数字前不能有前导0(单个0不算)。如果是一个表达式,则输出变化后的答案,如果不是一个表达式,则输出IMPOSSIBLE。

解法:

找规律,“不难发现”。我们只需要判断+0?的这种情况,让'?'变化为‘+’其余的全部为‘1’即可,然后再for循环跑一遍,看看他是否符合规律,也就是‘*’和‘+’不能连在一起最前和最后面都不能为‘+’或者‘*’,然后不能有前导0

附上AC代码(写到自闭):

#include <bits/stdc++.h>
using namespace std;
int main(){
	int t;
	char s[505];
	scanf("%d",&t);
	getchar();
	while(t--){
		gets(s);
		int len = strlen(s);
		for(int i=0;i<len;i++){
			if(s[i]=='?'){
				if(s[i-1]=='0'&&!('0'<=s[i-2]&&s[i-2]<='9'))
					s[i]='+';
				else
					s[i]='1';				
			}
		}
		int check=1;
		for(int i=0;i<len;i++){
			if(s[i]=='+'||s[i]=='*')
				if(i+1<len&&(s[i+1]=='*'||s[i+1]=='+'))
					check=0;
			if(s[i]=='0')
				if((s[i-1]>'9'||s[i-1]<'0')&&'0'<=s[i+1]&&s[i+1]<='9')
					check=0;
		}
		if(s[0]=='+'||s[0]=='*'||s[len-1]=='+'||s[len-1]=='*')
			check=0;
		if(check==1){
			for(int i=0;i<len;i++)
				printf("%c",s[i]);
			printf("\n");
		}else
			printf("IMPOSSIBLE\n");
	}
}
发布了22 篇原创文章 · 获赞 19 · 访问量 3547

猜你喜欢

转载自blog.csdn.net/KnightHONG/article/details/81354211
今日推荐