杭电多校第四场 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
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+* ?
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+*? .
If there are multiple answers, print any of them.
If it is impossible to find such an expression, print IMPOSSIBLE.
?????
0+0+0
?+*??
?0+?0
?0+0?
0+0+0
IMPOSSIBLE
10+10
IMPOSSIBLE
先考虑运算符不行的情况再考虑前导0的情况
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
string s;
vector<string> 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 ;
}
杭电多校第四场 Problem K. Expression in Memories 思维模拟的更多相关文章
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- [2019杭电多校第四场][hdu6623]Minimal Power of Prime
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623 题目大意为求一个数的唯一分解的最小幂次.即120=23*31*51则答案为1. 因为数字太大不能 ...
- [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...
- [2019杭电多校第四场][hdu6616]Divide the Stones
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6616 题意是说n个数分别为1-n,将n个数分成k堆,能否满足每堆个数相等,数值之和相等.保证n%k=0 ...
- [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...
- 2019杭电多校第四场hdu6623 Minimal Power of Prime
Minimal Power of Prime 题目传送门 解题思路 先打\(N^\frac{1}{5}\)内的素数表,对于每一个n,先分解\(N^\frac{1}{5}\)范围内的素数,分解完后n变为 ...
- 杭电多校第四场-H- K-th Closest Distance
题目描述 You have an array: a1, a2, , an and you must answer for some queries.For each query, you are g ...
- 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)
K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
随机推荐
- 聊一聊Java的枚举enum
一. 什么是枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景,如我们需要表达性别(男.女),颜色(红.黄.蓝),星期(星期一.星期二.. ...
- hdu 6406 Taotao Picks Apples (线段树)
Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n apples o ...
- 放出一批jsp图书管理系统图书借阅系统源码代码运行
基于jsp+mysql的JSP图书销售管理系统 https://www.icodedock.com/article/105.html基于jsp+Spring+Spring MVC的Spring图书借阅 ...
- 关于http 500错误的小结分享
一般情况下,http 500内部服务器(HTTP-Internal Server Error)错误说明IIS服务器无法解析ASP代码,访问一个静态页面试试是否也出现这个问题. 如果访问静态页面没问题, ...
- HelloDjango 系列教程:博客从“裸奔”到“有皮肤”
文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在此之前我们已经编写了博客的首页视图,并且配置了 URL 和模板,让 django 能够正确地处理 HTTP 请求并返回合适的 ...
- Go中的文件读写
在 Go 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄 .我们来看一下os包的使用方式. 1.读取文件 os包提供了两种打开文件的方法: Open(name string) ...
- Linux:oracle11.2.0dbca静默建库
1.关闭防火墙 systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall ...
- Map集合的遍历.
package collction.map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; im ...
- python小白手册之字符串的私有方法和公用方法
#字符串方法. name=input('1111') if name.isalnum(): print(是否由数字字母) isdigit isdecimal判断数字 strip去空格或者其他 name ...
- hive explode 行拆列
创建一张表test_explode,表结构如下 表数据如下: 1.使用explode函数 select explode(friends) as friend from test_explode; 但是 ...