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. linux下history命令显示历史指令记录的使用方法

    Linux系统当你在shell(控制台)中输入并执行命令时,shell会自动把你的命令记录到历史列表中,一般保存在用户目录下的.bash_history文件中.默认保存1000条,你也可以更改这个值 ...

  2. python字符编码(二)

    一.什么是字符编码 计算机要想工作必须通电,也就是说‘电’驱使计算机干活,而‘电’的特性,就是高低电压(高低压即二进制数1,低电压即二进制数0),也就是说计算机只认识数字 编程的目的是让计算机干活,而 ...

  3. 常见linux命令释义(第一天)

    快到中午吃饭了,然后忽然想起来samba里面没有添加用户.于是乎,就玩弄起了samba. Samba三下五除二就安装好了,想想window里面不断的点击下一步,还要小心提防各种隐藏再角落里的绑定软件. ...

  4. JQuery 学习笔记(01)

    JQuery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari 2.0+, Oper ...

  5. zabbix 3.0快速安装简介(centos 7)

    zabbix快速安装 系统版本:centos 7 通过yum方法安装Zabbix3.0,安装源为阿里云 yum源配置 rpm -ivh http://mirrors.aliyun.com/zabbix ...

  6. JQuery------判断拥有某个class或id的div是否存在

    if ($(".Btn,#Show").length > 0) { alert("存在"); } else { alert("不存在" ...

  7. ecshop广告-》单张,多张

    //读取广告 function get_ad_id($ad_id){ //读取指定ad_id广告 $sql = 'select * from '. $GLOBALS['ecs']->table( ...

  8. validate jquery 注册页面使用实例 详解

    官方使用文档:http://jqueryvalidation.org/documentation/ 参考资料:http://www.w3cschool.cc/jquery/jquery-plugin- ...

  9. TFS2012 安装 配置笔记

    TFS2012安装   具体请看文档..   http://yunpan.cn/cmt4X6S7TjEgq  访问密码 464e     TFS2012配置 环境:VS2012  SQL2008  T ...

  10. Java并发编程核心方法与框架-CompletionService的使用

    接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...