牡丹江.2014k(构造)
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
System Crawler (2015-08-15)
Description
Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.
To clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.
In this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.
You are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.
You task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:
- Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".
- Swap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".
The strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.
Output
For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.
Sample Input
3
1*1
11*234**
*
Sample Output
1
0
2
#include<bits/stdc++.h>
using namespace std; int star , dig ;
string s , t ; int main () {
int T ;
scanf ("%d" , &T ) ;
while (T --) {
t.clear () ;
star = ; dig = ;
cin >> s ;
int minn = ;
int n = s.size () ;
for (int i = ; i < n ; i ++) {
if ( s[i] == '*') star ++ ;
else dig ++ ;
}
if (star == ) {
puts ("") ;
continue ;
}
if (dig == ) {
printf ("%d\n" , star + ) ;
continue ;
}
if (s[n-] != '*') {
for (int i = ; i < n ; i ++) if (s[i] == '*') {s[i] = '' ; break ;}
s[n-] = '*' ;
minn ++ ;
}
if (star+ > dig ) minn += star+ - dig ;
for (int i = ; i < star+ - dig ; i ++) t += '' ;
t += s ;
n = t.size () ;
int _star = , _dig = ;
for (int i = ; i < n ; i ++) {
if (t[i] != '*') {
_dig ++ ;
}
else {
if (_dig > ) _dig -- ;
else {
_dig ++ ;
for (int i = n - ; i >= ; i --) if (s[i] != '*') {s[i] = '*' ; break ;}
minn ++ ;
}
}
}
cout << minn << endl ;
}
return ;
}
这道题在算之前,你首先要保证两个条件符合:1,最后一位为‘*’ ;2,当前序列的 “数字总数” > " ‘*'的总数“ 。
然后因为有了以上保证,现在进行交换操作肯定是最优的了,所以只要0(n)扫一遍,使每个‘*’都符合运算条件,不符合则与最后一个数字交换。
还要注意这里有个梗,当其全部是数字时,输出0 ;
牡丹江.2014k(构造)的更多相关文章
- cf251.2.C (构造题的技巧)
C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...
- 学习笔记:Maven构造版本号的方法解决浏览器缓存问题
需要解决的问题 在做WEB系统开发时,为了提高性能会利用浏览器的缓存功能,其实即使不显式的申明缓存,现代的浏览器都会对静态文件(js.css.图片之类)缓存.但也正因为这个问题导致一个问题,就是资源的 ...
- 一步步构造自己的vue2.0+webpack环境
前面vue2.0和webpack都已经有接触了些(vue.js入门,webpack入门之简单例子跑起来),现在开始学习如何构造自己的vue2.0+webpack环境. 1.首先新建一个目录vue-wk ...
- About 静态代码块,普通代码块,同步代码块,构造代码块和构造函数的纳闷
构造函数用于给对象进行初始化,是给与之对应的对象进行初始化,它具有针对性,函数中的一种.特点:1:该函数的名称和所在类的名称相同.2:不需要定义返回值类型.3:该函数没有具体的返回值.记住:所有对象创 ...
- Eos开发——构造查询条件
1.ajax 方式 var data = { orgid :orgid,year:year ,month: month,type:type,sortField:'sellEmpname' ,sortO ...
- 【C++】类和对象(构造与析构)
类 类是一种抽象和封装机制,描述一组具有相同属性和行为的对象,是代码复用的基本单位. 类成员的访问权限 面向对象关键特性之一就是隐藏数据,采用机制就是设置类成员的访问控制权限.类成员有3种访问权限: ...
- Spring 设值注入 构造注入 p命名空间注入
注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...
- 并发包的线程池第二篇--Executors的构造
上一篇讲述了ThreadPoolExecutor的执行过程,我们也能看出来一个很明显的问题:这个线程池的构造函数比较复杂,对于不十分理解其运作原理的程序员,自己构造它可能体现和想象中不一样的行为.比如 ...
- 10、代码块、构造代码块、静态代码块及main方法之间的关系
1.普通代码块: 在方法或语句中出现在{}之间的类容就称为普通代码块,简称代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”,即顺序执行. /*下面第一个类时合法的 ...
随机推荐
- jqGrid使用方法
1.下载文件 下载jqGrid的软件包,下载地址为:http://www.trirand.com/blog/?page_id=6 下载jQuery文件,下载地址为:http://code.jquery ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- 启动和关闭ADB服务(adb start-server和adb kill-server)
1 Android SDK中的常用命令行工具 在<Android SDK安装目录>\tools目录中带了很多命令行工具.虽然一般的开发人员并不需要完全掌握这些工具的使用方法,但了解这些工 ...
- Linux运维工程师入门的10大实用工具
说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具. 我就大概列出这几方面,这样入门就基本没问题了. 工具如下: 1.Linux系统基础 这个不用说了,是基础中的基础,连这 ...
- 【原创】一段简短的读取libglade的UI文件的Python代码
准备写一个将Glade/GtkBuilder等格式的UI文件转换成C++代码的python程序 首先完成的是将LIBGlade格式读取至内存中 #!/usr/bin/env python # -*- ...
- 9.19 JS数组
数组:相同类型数据的集合强类型语言:1数组里面只能存放相同数据类型的数据 2定义数组的时候需要制定一个长度(可以存放的元素数量) 3内存空间连续集合:1.可以存放任意类型的数据 ...
- tomcat十大安全优化措施
1.telnet管理端口保护 使用telnet连接进来可以输入SHUTDOWN可以直接关闭tomcat,极不安全,必须关闭.可以修改默认的管理端口8005改为其他端口,修改SHUTDOWN指令为其他字 ...
- app中Webview实现下载表格
<script type="text/javascript"> function getUrl(){ var close = confirm("请点击确定下载 ...
- JavaWeb学习笔记——开发动态WEB资源(一)Java程序向浏览器输出数据
开发一个动态web资源,即开发一个Java程序向浏览器输出数据,需要完成以下2个步骤: 1.编写一个Java类,实现Servlet接口 开发一个动态web资源必须实现javax.servlet.Ser ...
- 使用node的插件UglifyJs来合并和压缩文件
code: var fs = require('fs'); var jsp = require("./UglifyJS-master/uglify-js").parser; var ...