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. Build to win!——获得小黄衫的感想

    UPDATE: 应栋哥要求,上传了无遮挡的正面照(我的内心其实是拒绝的!(ㄒoㄒ)) 一.前言&背景 从大一上C++课程开始,栋哥就开始安利他大三的软工实践课. 时间过得飞快,大学转眼就过去一 ...

  2. RNN 入门教程 Part 4 – 实现 RNN-LSTM 和 GRU 模型

    转载 - Recurrent Neural Network Tutorial, Part 4 – Implementing a GRU/LSTM RNN with Python and Theano ...

  3. Chrome控制台 JS调试的一些小技巧

    $ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择过的DOM节点.在页面右击选择审查元素,然 ...

  4. React Native 开发之 (04) 例子讲解

    一.了解index.ios.js React-Native就是在开发效率和用户体验间做的一种权衡.React-native是使用JS开发,开发效率高.发布能力强,不仅拥有hybrid的开发效率,同时拥 ...

  5. Docker入门教程(二)命令

    Docker入门教程(二)命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第二篇,介绍了Docker的基本命令以及命令的用法和功能. 在Docker ...

  6. BZOJ4668: 冷战

    并查集,按秩合并,树高log,暴力查询. 果然bzoj新挂的题中过的人多的全是sb题. 写了一发秒WA,发现姿势不对.(@_@) 然后过了50min,开始怀疑人生.(*_*) 这么长时间我lct都写完 ...

  7. SQL Server编程(01)流程控制

    批处理 应用程序向SqlServer发送的一组命令,Sql Server会将其编译成一个可执行单元,称为执行计划,执行计划中的语句每次执行一条. 每个不同的批处理用GO命令分割.GO命令不是SQL语句 ...

  8. map遍历方法

    java中遍历MAP的几种方法 Java代码 Map<String,String> map=new HashMap<String,String>();    map.put(& ...

  9. IIS Express 外部访问

    http://blog.csdn.net/zhangjk1993/article/details/36671105

  10. Linq------各种查询语句大全

    查询Title列的第一个值 string str = db.Webs.Select(p => p.Title).FirstOrDefault(); 根据ID,查询Title列的第一个值 b.We ...