ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
题目链接: problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383
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:
- 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".
- 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
题意:
给出两个操作:插入和随意交换字符串两个字母;
求最少的操作数使得字符串变成后缀表达式,
PS:
仅仅有缺少数字的时候才会用到插入,其它就是交换操作,把*都交换到后面。而一个*须要两个数字。
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2017;
int main()
{
int t;
char s[maxn];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int len = strlen(s);
int cont_num = 0, cont_star = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '*')
cont_star++;
else
cont_num++;
}
if(len == cont_num)
{
printf("0\n");
continue;
}
int k = cont_star-cont_num+1;
if(k < 0)
k = 0;
int star = 0;
int num = k;
int ans = k;
int re[maxn];
for(int i = len-1, l = 0; i >= 0; i--)
{
if(s[i]!='*')
re[l++] = i;
}
int l = 0;
for(int i = 0; i < len; i++)
{
if(s[i]=='*')
star++;
else
num++;
if(star+1 > num)
{
swap(s[i],s[re[l++]]);
ans++;
star--;
num++;
}
}
if(s[len-1] != '*')
ans++;
printf("%d\n",ans);
}
return 0;
}
ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)的更多相关文章
- zoj 3829 Known Notation
作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...
- 2014ACMICPC亚洲区域赛牡丹江现场赛之旅
下午就要坐卧铺赶回北京了.闲来无事.写个总结,给以后的自己看. 因为孔神要保研面试,所以仅仅有我们队里三个人上路. 我们是周五坐的十二点出发的卧铺,一路上不算无聊.恰巧邻床是北航的神犇.于是下午和北航 ...
- 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结
不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
Known Notation Time Limit: 2 Seconds Memory Limit: 131072 KB Do you know reverse Polish notatio ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
随机推荐
- [Swift]LeetCode1071.字符串的最大公因子 | Greatest Common Divisor of Strings
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- php实时推送系统消息给客户端
在我们实际开发过程中,有些数据需要实时获取:比如erp系统中的订单信息,OA系统中的流程审批等都需要及时处理,这时我们就不能再使用http协议了:当然也可以使用轮询的机制.但是轮询请求中有大半是无用, ...
- PHP操作多进程
在以往的开发项目中,要操作进程就会使用PHP自带的pcntl拓展.但是pcntl存在着许多的不足: pcntl没有提供进程间通信的功能 pcntl不支持重定向标准输入和输出 pcntl只提供了fork ...
- 5.17从零开始搭建springboot-dubbo的例子
https://www.cnblogs.com/baijinqiang/p/10848259.html
- mysql数据库之存储过程入门
引用:百度百科 存储过程 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存 ...
- kotlin第一个项目的搭建
怎么在Android Studio中使用Kotlin? 1.使用Android Studio的插件 2.将Android Studio升级到3.0版本:目前不推荐,因为3.0的版本目前还是Dev Ch ...
- js-常见简单的js判断方法(暂不参考正则)
1: 2: 3: 4: 5: 6: 7:
- java程序员级别划分
IT路虽好,却难走.1级 为会基本语法 大学里的JAVA教程 能及格 2级 自己可以写个 俄罗斯方块,扫雷,贪吃蛇, 拼图之类的小游戏 3级 能够进手机游戏CP,SP公司,做手机游戏 或者 ...
- dubbo之分组聚合
按组合并返回结果 ,比如菜单服务,接口一样,但有多种实现,用group区分,现在消费方需从每种group中调用一次返回结果,合并结果返回,这样就可以实现聚合菜单项. 相关代码可以参考 dubbo 项目 ...
- MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码)
MySQL基础配置之mysql的默认字符编码的设置(my.ini设置字符编码) MySQL的默认编码是Latin1,不支持中文,那么如何修改MySQL的默认编码呢,下面以设置UTF-8为例来说明. 需 ...