博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
杭电多校第四场 Problem K. Expression in Memories 思维模拟
阅读量:5999 次
发布时间:2019-06-20

本文共 2962 字,大约阅读时间需要 9 分钟。

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 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

先考虑运算符不行的情况再考虑前导0的情况 

 

AC代码
#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ls (r<<1)#define rs (r<<1|1)#define debug(a) cout << #a << " " << a << endlusing namespace std;typedef long long ll;const ll maxn = 1e5 + 10;const ll mod = 1e9 + 7;string s;vector
e;bool iso( char c ) { if( c == '+' || c == '*' ) { return true; } return false;}int main() { ll T; cin >> T; while( T -- ) { cin >> s; bool flag = true; for( ll i = 0; i < s.length(); i ++ ) { if( ( i == 0 || i == s.length()-1 ) && iso(s[i]) ) { flag = false; break; } if( i < s.length()-1 ) { if( iso(s[i]) && iso(s[i+1]) ) { flag = false; break; } } if( s[i] == '?' ) { if( s[i-1] == '0' && ( i-2 < 0 || iso(s[i-2]) ) && !iso(s[i+1]) ) { s[i] = '+'; } else { s[i] = '1'; } } } //debug(s); e.clear(); string t = ""; for( ll i = 0; i < s.length(); i ++ ) { if( iso(s[i]) || i == s.length()-1 ) { if( i == s.length()-1 ) { t += s[i]; } e.push_back(t); t = ""; } else { t += s[i]; } } for( ll i = 0; i < e.size(); i ++ ) { //cout << e[i] << endl; if( e[i][0] == '0' && e[i].length() > 1 ) { flag = false; break; } } if( flag ) { cout << s << endl; } else { cout << "IMPOSSIBLE" << endl; } } return 0 ;}

  

转载于:https://www.cnblogs.com/l609929321/p/9402017.html

你可能感兴趣的文章
lua连接redis集群
查看>>
SecureCRT快捷键
查看>>
Docker端口映射及创建镜像演示(二)--技术流ken
查看>>
MongoDB最新驱动解析
查看>>
临沭吴忠军百科
查看>>
如何在注册表被锁定的情况下修复注册表
查看>>
ruby连接redis
查看>>
【转】关于社交网络的十五段废话
查看>>
决策树算法
查看>>
Android_传感器光学
查看>>
日志系统
查看>>
maven ssm pom.xml
查看>>
正则大全
查看>>
mui页面传值
查看>>
mysql注入
查看>>
Andriod ----配置环境变量
查看>>
static variable
查看>>
SlickUpload使用(二)
查看>>
菊花加载第三方--MBprogressHUD
查看>>
假期 题解
查看>>