HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories

Posted qq965921539

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU6342-2018ACM暑假多校联合训练4-1011-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): 0    Accepted Submission(s): 0
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 (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

题意就是让你在?里添加上合适的1或者+,并判断该式子是否合法(语序有一点错误)

注意在类似 +0? 的情况下, ? 须被替换为 + 或 * ,其余情况直接将 ? 替换为非零数字就好。替换完成后判断一下是否合法。

                                                     ———HDU题解

全靠队友一条大腿,当时WA到死都没找到0??1这个错误

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 char ch[510];
 7 int ch1[510];
 8 int main()
 9 {
10     ios::sync_with_stdio(false);
11     int t;
12     cin >> t;
13     while (t--)
14     {
15         cin >> ch;
16         bool flag = 0;
17         for (int i = 0; i < strlen(ch); i++)
18         {
19             if (i > 1 && (ch[i - 2] == + || ch[i - 2] == *) && ch[i - 1] == 0 && ch[i] == ?)
20                 ch[i] = +;
21             if (i == 1 && ch[i] == ? && ch[i - 1] == 0)
22                 ch[i] = +;
23             else if(ch[i] == ?)
24                 ch[i] = 1;
25         }
26         if (ch[strlen(ch) - 1] == + || ch[strlen(ch) - 1] == * || ch[0] == + || ch[0] == *)
27             flag = 1;
28         else
29         {
30             for (int i = 0; i < strlen(ch); i++)
31             {
32                 if (i == 1 && ch[i - 1] == 0 && ch[i] >= 0 && ch[i] <= 9)
33                     flag = 1;
34                 else if (i > 1 && ch[i - 1] == 0 && (ch[i - 2] == + || ch[i - 2] == *) && ch[i] >= 0 && ch[i] <= 9)
35                     flag = 1;
36                 else if ((ch[i - 1] == + || ch[i - 1] == *) && (ch[i] == + || ch[i] == *))
37                     flag = 1;
38             }
39         }
40         if(flag)
41             cout << "IMPOSSIBLE" << endl;
42         else
43             cout << ch << endl;
44     }
45     return 0;
46 }

 

以上是关于HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories的主要内容,如果未能解决你的问题,请参考以下文章

HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

暑假N天乐比赛篇 —— 2019杭电暑期多校训练营(第五场)

2015暑假多校联合---Mahjong tree(树上DP 深搜)

2015暑假多校联合---Expression(区间DP)

2015暑假多校联合---CRB and His Birthday(01背包)