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

本文介绍了一个编程挑战,旨在修复包含未知字符的数学表达式。通过替换特定符号,确保每个表达式都符合预定义的语法规则,并探讨了有效的解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KnightHONG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值