HDU6342 杭电多校第四场 Problem K. Expression in Memories 思维模拟

Problem K. Expression in Memories

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

 

 

Source

2018 Multi-University Training Contest 4

 

【题意】:给你一个字符串,问你能否构成一个合法的表达式,可以的话输出其中之一,不可以的话输出IMPOSSIBLE,表达式由数字,+,*组成

 

【思路】:可能最近脑子比较混,一道思维题,首先考虑能不能构成数字符号数字,再考虑有没有前导0的情况,比赛的时候思维十分混乱,一堆Bug.....

 

AC代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
int main ()
{
	int t;
	char s[509];
	scanf("%d",&t);
	while (t--){
		memset(s,0,sizeof(s));
		scanf("%s",s+1);
		int len = strlen(s+1);
		int vis = 0;
		if ((s[1] < '0' || s[1] > '9') && s[1] != '?') {
			printf("IMPOSSIBLE\n");
			continue;
		}
		if ((s[len] < '0' || s[len] > '9') && s[len] != '?') {
			printf("IMPOSSIBLE\n");
			continue;
		}
		if (((s[len-2] < '0' || s[len-2] > '9') && s[len-2] != '?')&&s[len-1] == '0'){
			printf("IMPOSSIBLE\n");
			continue;
		}
		if (s[1] == '0') vis = 1;
		if (s[1] == '?') s[1] = '1';
		int flag = 0;
		int k = 0;
		for (int i=  2; i <= len; i++){
			if (flag){
				if (s[i] == '0') vis = 1,flag = 0;
				else vis = 0,flag = 0;
				if ((s[i] < '0' || s[i] > '9') && s[i] != '?') {
					printf("IMPOSSIBLE\n");
					k = 1;
					break;
				}
				if (s[i] == '?'){
					s[i] = '1';
				}
				continue;
			}
			if (vis) {
				if (s[i] >= '0' && s[i] <= '9') {
					printf("IMPOSSIBLE\n");
					k = 1;
					break;
				}
				else if (s[i] == '?' && s[i+1] != '+' && s[i+1] != '*'){
					s[i] = '+';
					flag = 1;
					vis = 0;
				}
				else if (s[i] == '?' && (s[i+1] != '+' || s[i+1] != '*')){
					printf("IMPOSSIBLE\n");
					k = 1;
					break;
				}
				else vis = 0;
			}
			if ((s[i] < '0' || s[i] > '9') && s[i] != '?') {
				flag = 1;
			}
			else{
				flag = 0;
				if (s[i] == '?')
					s[i] = '1';
			}
		}
		if ((s[len] < '0' || s[len] > '9') && s[len] != '?') {
			printf("IMPOSSIBLE\n");
			continue;
		}
		if (!k){
			for (int i =1 ; i <= len; i++) printf("%c",s[i]);
			printf("\n");
		}
	}
}

 

你可能感兴趣的:(ACM,HDU,模拟)