题目大意:给你一个不完整的后缀表达式和两种操作,一是插入一个数或者“*”运算号,另一个是把数字和“*”交换、

一个*至少对应两个数字,而一个表达式又等于一个数字,所以相当于运算符号和数字个数相等即可(特殊情况暂且不算)。

那么对于符号数大于数字个数的情况,我们就先把数字补在最前面。(贪心的思想,因为表达式的判定方式是两个数字在前,运算符在后)

然后从前往后按对儿消去,如果运算符前面的数字或者表达式不够两个,那么就把运算符和最后面的数字调换位置即可。(看了别人的想法之后,才发现不用手动换,用栈模拟效果不错)

Known Notation


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

Author: CHEN, Cong
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
#include <string>
using namespace std;
#define MAXN 5000
char ch[MAXN];
int t, n, n1, n2, ans;
int main() {scanf("%d", &t);while(t--) {string str;stack<int> sta;//记录所有数字下标,用于交换操作scanf("%s", ch);n1 = 0, n2 = 0;for(int i = 0; i < strlen(ch); i++)if(ch[i] == '*') n2++;else n1++;if(n2 == 0){printf("0\n"); goto loop;}for(int i = 0; i <= n2 - n1; i++) str += "1";str += ch;for(int i = 0; i < str.length(); i++)if(str[i] != '*') sta.push(i);n = 0, ans = max(n2 - n1 + 1, 0);for(int i = 0; i < str.length(); i++) {if(str[i] == '*') {if(n < 2) {str[sta.top()] = '*';sta.pop();n++; ans++;} else n--;} else n++;}if(ans == 0 && str[str.length() - 1] != '*') ans = 1;printf("%d\n", ans);loop:;}return 0;
}

转载于:https://www.cnblogs.com/gaoxiang36999/p/4451504.html

ZOJ 3822 Known Notation(2014牡丹江Regional K题)相关推荐

  1. Known Notation 39届亚洲赛牡丹江站K题

    题意:       题意,哎!说道题意就蛋疼啊,比赛的时候就愣是把这个题目读成数字可以随意组合,比如123 可以拆成1 23 ,12 3 ,1 2 3,结果显然,水题当神题,各种想不出来,然后就显然的 ...

  2. Dream_Chaser队训练赛第一场 K题

    Dream_Chaser队训练赛第一场 K题 题目来自2012成都区域赛 K - Yet Another Multiple Problem Time Limit:20000MS     Memory ...

  3. 2014年华为上机题及代码

    http://blog.csdn.net/dalianmaoblog/article/details/11477997 题目来源于http://blog.csdn.net/hackbuteer1/ar ...

  4. Building Fire Stations 39届亚洲赛牡丹江站B题

    题意:      给你一棵树,让你再里面选取两个点作为**点,然后所有点的权值是到这两个点中最近的那个的距离,最后问距离中最长的最短是多少,输出距离还有那两个点(spj特判). 思路:      现场 ...

  5. 2021电赛国一——K题照度稳定可调LED台灯

    目录 一.数字显示照度表 1.1.效果图 1.2.简介及程序 二.台灯整体程序 2.1.程序流程图 2.2.主函数main 2.3.自动调节程序 三.手机APP 3.1.手机APP界面效果图 3.2. ...

  6. 2021牛客暑假多校第二场 K题—Stack (链表)

    2021牛客暑假多校第二场 K题-Stack 题意: 一个单调栈,给你第n次操作时里面数据的数量,让你给出里面塞入的会是哪些数字. 主要思想:链表模拟 (代码里面有注释) (题解一开始说的是拓扑,后来 ...

  7. 照度稳定可调 LED 台灯(K 题)【高职高专组】-- 2021 年全国大学生电子设计竞赛

    照度稳定可调 LED 台灯(K 题)[高职高专组]-- 2021 年全国大学生电子设计竞赛 一 任务 二 要求 1.基本要求 2.发挥部分 三 说明 四 评分标准 优秀作品开源参考(来源立创开源平台) ...

  8. 中考计算机flash试题及答案,2014年中考信息技术题库试题Flash知识点.doc

    2014年中考信息技术题库试题Flash知识点 知识点手册-6 Flash知识点 一.基础: 1.软件的功能:动画制作 2.扩展名:fla 3.特点:交互功能强,基于矢量图形技术,不会失真.(位图放大 ...

  9. 单相用电器分析监测装置(K 题 本科组)-- 2017 年全国大学生电子设计竞赛试题

    单相用电器分析监测装置(K 题 本科组)-- 2017 年全国大学生电子设计竞赛试题 一.任务 二.要求 1.基本要求 2.发挥部分 三.说明 四.评分意见 一.任务 设计并制作一个可根据电源线的电参 ...

最新文章

  1. jQuery选择器之动态列表显示Demo
  2. OkHttp实现文件上传进度
  3. Imageio: 'ffmpeg-win32-v3.2.4.exe' was not found on your computer; downloading it now.
  4. C++实现折半插入排序
  5. 第十章:在Spark集群上掌握比较重要的图操作之Computing Degree
  6. eclipse或Myeclipse中web项目没有run on server时怎么办?
  7. BufferedReader类
  8. 2018通达信l2服务器源码,通达信强势龙头指标源码无未来,牛股连板涨停启动源码...
  9. Zabbix监控系统系列之九:监控网络设备指定接口流量
  10. ESP8266恒温控制器
  11. JAVA输入任意一个数,判断是否是回文数
  12. outlook如何撤回邮件?(中英文)
  13. Oracle域完整性约束
  14. 手机续航能力测试软件,五小时极限测试告诉你荣耀X10续航表现怎么样
  15. Discord账号被封怎么办?Discord账号解封申诉方案
  16. Web 前端面试题总结
  17. Java--面向对象_中
  18. GraphSAGE NIPS 2017 代码分析(Tensorflow版)
  19. phpstorm 配置 xdebug断点调试
  20. 西方哲学智慧2018网课考题(本人亲测,已满分)

热门文章

  1. 【深度学习】图像自动处理工具SimpleITK的使用(Python)
  2. 【Java Web开发指南】mybatis的Example[Criteria]的使用
  3. 【Java Web开发指南】Spring一些基础问题整理
  4. 机器学习(MACHINE LEARNING)MATLAB经济金融领域简单数学模型和分析
  5. 解决module ‘numpy‘ has no attribute ‘array‘问题
  6. vue hot true 不起作用_从源码解读 Vuex 注入 Vue 生命周期的过程
  7. java不能修改表_java中不可修改列表的类型是什么
  8. bootstrap-fileinput 添加打印按钮
  9. 算法与数据结构(2)
  10. 网站推广——网站推广优化期间突然发现网站收录降低怎么回事?