任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342

Problem K. Expression in Memories

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

题意概括:

给一串表达式(可能完整可能不完整),表达式只含有 ‘+’ 和 ‘ * ’ 两种运算,数字为 0~9;

如果不完整(含' ? '), 则补充完整。

若表达式本身非法或者无法补充成为一个合法表达式,则输出“IMPOSSIBLE”

解题思路:

很明显 “ ?” 只有在 0?的情况下需要变成 ‘+’ 或者‘*’; 其他情况都把 “ ?”变成 非0的数字即可。

判断表达式是否合法: 是否出现 0111 或者 ++ 或者 *+ 这类的情况即可。

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const LL MOD = 1e9+;
const int MAXN = ;
char str[MAXN];
//char ans[MAXN]; int main()
{
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%s", str);
int len = strlen(str);
bool flag = true; for(int i = ; i < len; i++){
if(str[i] == '+' || str[i] == '*'){
if(i == || i == len-){
flag = false;
break;
}
else if(str[i+] == '+' || str[i+] == '*'){
flag = false;
break;
}
}
else if(str[i] == ''){
if(i == || str[i-] == '+' || str[i-] == '*'){
if(i < len- && str[i+] >= '' && str[i+] <= ''){
flag = false;
break;
}
else if(i < len- && str[i+] == '?'){
str[i+] = '+';
}
}
}
else if(str[i] == '?'){
str[i] = '';
}
} if(flag) printf("%s\n", str);
else puts("IMPOSSIBLE");
}
return ;
}

2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】的更多相关文章

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

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  2. HDU 6342.Problem K. Expression in Memories-模拟-巴科斯范式填充 (2018 Multi-University Training Contest 4 1011)

    6342.Problem K. Expression in Memories 这个题就是把?变成其他的使得多项式成立并且没有前导零 官方题解: 没意思,好想咸鱼,直接贴一篇别人的博客,写的很好,比我的 ...

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

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

  4. HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/500 ...

  5. 2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/100 ...

  6. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  7. 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Problem L. Graph Theory Homework Time Limit: 2000 ...

  8. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  9. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

随机推荐

  1. mysql,简单介绍一下索引

    汉字很多,人力有时尽,人不可能记住所有的字,为了解决这个问题,于是有了字典.数据库里的数据很多,为了方便检索,于是有了索引. 索引,是一种数据结构,在这种数据结构中实现了高级的查找算法,索引可以帮助我 ...

  2. Filter的常见应用

    1.字符编码过滤器 实现功能,在a.jsp中填写用户名提交到b.jsp,在b.jsp中读取参数名. a.jsp <body> <form action="encoding/ ...

  3. class文件反编译工具jd-gui下载地址

    https://github.com/java-decompiler/jd-gui/releases windows下载: 下载后打开软件,直接将jar包拖进去: 效果图非常美观:

  4. C Primer Plus note1

    C语言编译错误:multiple definition of `main' main多重定义,在同一个工程中定义了多个main函数 出现如下图的错误: 这是因为在第一张图中,有一个main.c的mai ...

  5. postgres备份数据库

    1. psql --help psql is the PostgreSQL interactive terminal. Usage: psql [OPTION]... [DBNAME [USERNAM ...

  6. unity配置Android SDK,并构建导出apk格式

    1.点击 Edit  -->   preferences出现如图界面 2.在Android中SDK选项中放入Android SDK:JDK选项中放入Java JDK.(下载地址:http://t ...

  7. 使用cocostudio 需要在Android.mk文件的配置

    直接贴上Android.mk文件吧. 对了,是cocos2d3.0的,不知道2.x是否一样. LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LO ...

  8. SQL Server ->> Enable Instant File Initialization(开启文件及时初始化)

    我们在安装SQL Server的时候会在指定SQL Server各个服务的运行启动账户的账户的时候看到底下有一个选项写着“Grant Perform Volume Maintenance Task p ...

  9. SQL Server ->> PARSE函数

    这个函数和TRY_PARSE一起从SQL Server 2012引入.它的存在是因为TRY_PARSE一旦遇到无法成功转换就会以NULL值返回,而如果你希望以报错的形式,你就可以用PARSE. 比如 ...

  10. 爬虫入门之Scrapy框架实战(新浪百科豆瓣)(十二)

    一 新浪新闻爬取 1 爬取新浪新闻(全站爬取) 项目搭建与开启 scrapy startproject sina cd sina scrapy genspider mysina http://roll ...