K - Known Notation

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Appoint description: 
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:

  1. 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".
  2. 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(构造)的更多相关文章

  1. cf251.2.C (构造题的技巧)

    C. Devu and Partitioning of the Array time limit per test 1 second memory limit per test 256 megabyt ...

  2. 学习笔记:Maven构造版本号的方法解决浏览器缓存问题

    需要解决的问题 在做WEB系统开发时,为了提高性能会利用浏览器的缓存功能,其实即使不显式的申明缓存,现代的浏览器都会对静态文件(js.css.图片之类)缓存.但也正因为这个问题导致一个问题,就是资源的 ...

  3. 一步步构造自己的vue2.0+webpack环境

    前面vue2.0和webpack都已经有接触了些(vue.js入门,webpack入门之简单例子跑起来),现在开始学习如何构造自己的vue2.0+webpack环境. 1.首先新建一个目录vue-wk ...

  4. About 静态代码块,普通代码块,同步代码块,构造代码块和构造函数的纳闷

    构造函数用于给对象进行初始化,是给与之对应的对象进行初始化,它具有针对性,函数中的一种.特点:1:该函数的名称和所在类的名称相同.2:不需要定义返回值类型.3:该函数没有具体的返回值.记住:所有对象创 ...

  5. Eos开发——构造查询条件

    1.ajax 方式 var data = { orgid :orgid,year:year ,month: month,type:type,sortField:'sellEmpname' ,sortO ...

  6. 【C++】类和对象(构造与析构)

    类 类是一种抽象和封装机制,描述一组具有相同属性和行为的对象,是代码复用的基本单位. 类成员的访问权限 面向对象关键特性之一就是隐藏数据,采用机制就是设置类成员的访问控制权限.类成员有3种访问权限: ...

  7. Spring 设值注入 构造注入 p命名空间注入

    注入Bean属性---构造注入配置方案 在Spring配置文件中通过<constructor-arg>元素为构造方法传参 注意: 1.一个<constructor-arg>元素 ...

  8. 并发包的线程池第二篇--Executors的构造

    上一篇讲述了ThreadPoolExecutor的执行过程,我们也能看出来一个很明显的问题:这个线程池的构造函数比较复杂,对于不十分理解其运作原理的程序员,自己构造它可能体现和想象中不一样的行为.比如 ...

  9. 10、代码块、构造代码块、静态代码块及main方法之间的关系

    1.普通代码块: 在方法或语句中出现在{}之间的类容就称为普通代码块,简称代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”,即顺序执行. /*下面第一个类时合法的 ...

随机推荐

  1. SQL Server 分页

    select top "+pageSize+" * from Table where id not in (select top "+(currentPage - 1) ...

  2. 网络存储(三)之ISCSI搭建的入门

    搭建iscsi 我们就拿两台linux服务器来做吧, 服务器系统均为CentOs6.6 64位的,信息如下 IP 安装的软件 192.168.22.142 iscsi target端:scsi-tar ...

  3. 深入JVM-java虚拟机的基本结构

    本文将介绍Java虚拟机的基本结构,各组成部分的作用,以及相互之间是如何协调的.而要了解这些,首先必须了解Java堆.Java栈.永久区和元数据区的基本概念. 一.Java虚拟机的架构 1.1 类加载 ...

  4. HTML CSS + DIV实现整体布局

    HTML CSS + DIV实现整体布局 1.技术目标: 开发符合W3C标准的Web页面 理解盒子模型 实现DIV+CSS整体布局 2.什么是W3C标准? W3C:World Wide Web Con ...

  5. HTTPS 客户端验证 服务端证书流程

    网上的文章很多, 但是对摘要的验证流程不够通俗易懂. QQ截图20160420114804.png 证书预置和申请 1:客户端浏览器会预置根证书, 里面包含CA公钥2:服务器去CA申请一个证书3: C ...

  6. [Java]double初始化问题

    如下: 1. 直接初始化 double[][] embossFilter = {{-1/9, 0, 1/9}, {-1/9, 1/9, 1/9}, {-1/9, 0, 1/9}}; 2. 赋值初始化 ...

  7. python--文件删除、判断目录存在、字符串替换

    昨晚笔试了金山WPS的测试开发卷,做个笔记(但不是答案,只是我的想法),关于文件和字符串的处理正在写入与完善至上一篇的博客中,现在题目如下: 1.使用脚本语言删除E:\abc目录下的所有文件: 利用o ...

  8. JavaScript学习笔记——函数

    javascript函数的声明和调用 将完成某一特定功能的代码集合起来,可以重复使用的代码块. 一.函数的声明方式(创建) A.基本语法 function 关键字 function 函数名([参数1] ...

  9. (转)Java中的String为什么是不可变的

    转自:http://www.importnew.com/7440.html String是所有语言中最常用的一个类.我们知道在Java中,String是不可变的.final的.Java在运行时也保存了 ...

  10. 欧几里德算法 GCD

    递归: int gcd(int a,int b) { ?a:gcd(b,a%b); } 非递归: int gcd(int m,int n) { int r; ) { m=n; n=r; } retur ...