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. python 集合、函数和文件操作

    1.set集合 set集合是一个无序.不可重复.可嵌套的序列,基本功能是进行成员关系测试和删除重复元素,可以使用大括号({})或者 set()函数创建集合,注意:创建一个空集合必须用 set() 而不 ...

  2. codevs 1432 总数统计

    1432 总数统计 时间限制: 1 s空间限制: 128000 KB题目等级 : 钻石 Diamond   题目描述 Description 给出n个数,统计两两之和小于k的方案数之和. 输入描述 I ...

  3. [转]Zookeeper原理及应用场景

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现同步服务,配置维护和命名服务等.Zookeeper是hadoop的一个子项目,其 ...

  4. HTML5学习总结-05 HTML5表单

    一 HTML5 新的类型 HTML5 拥有多个新的表单输入类型.这些新特性提供了更好的输入控制和验证. email url number range Date pickers (date, month ...

  5. PHP 生成图片缩略图函数

    <?php /** * 生成缩略图函数(支持图片格式:gif.jpeg.png和bmp) * @author ruxing.li * @param string $src 源图片路径 * @pa ...

  6. 爬虫5 html下载器 html_downloader.py

    #coding:utf8 import urllib2 __author__ = 'wang' class HtmlDownloader(object): def download(self, url ...

  7. Java——其他容器

    除了JFrame表示之外,还有其他几种常见的窗体:JPanel.JSplitPane.JTabbedPane.JScrollPane.JDesktopPane.JInternalFrame等. imp ...

  8. JavaScript学习笔记——基本知识

    JavaScript学习的教程来自后盾网 1>JavaScript的放置和注释 1.输出工具 A.alert(); B.document.write(); C.prompt("&quo ...

  9. Ankh不起作用的解决方法

    请检查Visual Studio 2008的Tool > Options... > Source Control,在下拉菜单中选择插件.

  10. 以全局监听的方式处理img的error事件

    http://www.ovaldi.org/2015/09/11/%E4%BB%A5%E5%85%A8%E5%B1%80%E7%9B%91%E5%90%AC%E7%9A%84%E6%96%B9%E5% ...