当前位置:   article > 正文

2018暑假多校赛【第四场】【字符串】-Problem K. Expression in Memories-YZHHHHHHH_2018暑假多校4

2018暑假多校4

Problem K. Expression in Memories

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


题意:

有一个表达式,由数字、操作符和问号表示,问号可以用  1~9  和 两个操作符来替代,

如果   表达式合法: 1、无前导零  2、操作符不相邻  3、第一位和最后一位不为操作符

    输出该表达式

否则

    输出   IMPOSSIBLE

注意几种情况,除了样例外给几组数据测试: 

1、0?

2、0?0??

3、100?+1

代码:

(代码略长)

  1. #include <stdio.h>
  2. #include <string.h>
  3. using namespace std;
  4. int main(){
  5. int t;
  6. while (scanf("%d",&t) != EOF){
  7. getchar();
  8. while (t--) {
  9. char s[1005];
  10. gets(s);
  11. int len = strlen(s);
  12. int sigh = 0;
  13. s[len] = '+';
  14. for (int i = 0; i < len; i++){
  15. if (s[i] == '?'){
  16. if (i == 0) s[i] = '1';
  17. else if (i == 1) {
  18. if (s[i-1] == '0') s[i] = '+';
  19. else s[i] = '1';
  20. }
  21. else {
  22. if((s[i-1]=='0'&&(s[i-2] >= '0' && s[i-2] <= '9'))||s[i-1]!='0') s[i]='1';
  23. else s[i]='+';
  24. }
  25. }
  26. }
  27. for (int i = 0; i < len; i++){
  28. if(i==0){
  29. if(s[i]=='0') if(s[i+1] >= '0' && s[i+1] <= '9') {
  30. sigh = 1;
  31. }
  32. if(s[i]=='+'||s[i]=='*') sigh=1;
  33. }
  34. else{
  35. if((s[i]=='+'||s[i]=='*')&&(s[i+1]=='+'||s[i+1]=='*')) sigh=1;
  36. if(s[i]=='0'&&(s[i-1]=='+'||s[i-1]=='*')&& (s[i+1] >= '0' && s[i+1] <= '9')){
  37. sigh = 1;
  38. }
  39. }
  40. }
  41. //puts(s);
  42. if (sigh) printf("IMPOSSIBLE\n");
  43. else {
  44. for (int i = 0; i < len; i++){
  45. printf("%c",s[i]);
  46. }
  47. printf("\n");
  48. }
  49. }
  50. }
  51. }

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/451467
推荐阅读
相关标签
  

闽ICP备14008679号