2014牡丹江K Known Notation
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:
- 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
看来只要认真思考,这些较为简单的题都是可以A的。
想了一个上午,一开始没把逆波兰表达式的可能性想清楚,导致算法错误,后来想到只要前面数字的个数大于*的个数,都是成立的,而且交换要比增加效率高。只有当数字个数小于*个数的时候才会增加数字,而且增加在最前面,这样是最优方案。之后只要发现到某一位*的个数大于等于数字的个数了,就把*和最后一个数字交换就可以了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define M(a,b) memset(a,b,sizeof(a))
typedef long long LL;
using namespace std; char num[]; int main()
{
int t;
int ans = ;
scanf("%d",&t);
while(t--)
{
scanf("%s",num);
int sl = strlen(num);
int cnt1 = ;
int cnt2 = ;
int flag = -;
int bu = ;
int seg = ;
int step = ;
int save = -;
for(int i = sl-;i>=;i--)
{
if(num[i]=='*')
save = i;
}
if(num[sl-] != '*')
{
if(save!=-)
{swap(num[sl-],num[save]);
step++;}
}
int tm1 = ;
int tm2 = ;
int ed;
for(int i = ;i<sl;i++)
{
if(num[i]=='*') tm1++;
else tm2++;
}
if(tm1>=tm2) {
step+=(tm1-tm2+);
cnt2=(tm1-tm2+);
ed = cnt2;
}
//cout<<ed<<endl;
for(int i = ;i<sl;i++)
{
if(num[i]=='*')
{
cnt1++;
if(cnt1>=cnt2)
{
int te1,te2;
for(int p = ;p<sl;p++)
if(num[p] != '*') te1 = p;
for(int q = sl-;q>=;q--)
if(num[q] == '*') te2 = q;
swap(num[te1],num[te2]);
step++;
cnt1 = ;
cnt2 = ed;
i = -;
continue;
}
}
else cnt2++;
}
printf("%d\n",step);
}
return ;
}
2014牡丹江K Known Notation的更多相关文章
- 2014 牡丹江区域赛 B D I
http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...
- 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 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- [2014 Regional]牡丹江 H Hierarchical Notation 做题记录
主妇:老年人谁是炮灰牡丹江,我们的团队只是做同步大赛 他决定开爆震H什么时候,A 5min 1Y.I在该限制后,纠结了很久30min+ 1Y,神继续承担各种位置卡D在,hpp见B我认为这是非常熟悉的研 ...
- 2014牡丹江——Hierarchical Notation
problemId=5380" style="background-color:rgb(51,255,51)">题目链接 字符串模拟 const int MAXN ...
- 2014牡丹江——Known Notation
题目链接 题意: 输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*).字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数.也能够看成连续的数,即能够随意加入空格. 如今有两 ...
- The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)
The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...
- 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
随机推荐
- springMVC 基于注解的controller
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- checking for tgetent()... configure: error: NOT FOUND!
今天centos出现了下面的异常: checking for tgetent()... configure: error: NOT FOUND! You need to install a termi ...
- HDU 1698 Just a Hook(线段树/区间更新)
题目链接: 传送门 Minimum Inversion Number Time Limit: 1000MS Memory Limit: 32768 K Description In the g ...
- HDU 3466 Proud Merchants(01背包问题)
题目链接: 传送门 Proud Merchants Time Limit: 1000MS Memory Limit: 65536K Description Recently, iSea wen ...
- Enlisting multiple 1-phase aware participants in the same transaction
In some cases it may be necessary to enlist participants that aren't two-phase commit aware into a t ...
- Linux系统被入侵后处理经历
服务器托管在外地机房. 突然,频繁收到一组服务器ping监控不可达邮件,赶紧登陆zabbix监控系统查看流量状况. 可见流量已经达到了800M左右,肯定不正常,马上尝试SSH登陆系统,不幸的事,这种情 ...
- 设计模式-14 MVC模式
一 MVC设计模式 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式,它是一个存在于服务器 表达层的模型,它将应用分开,改变应用之间的高度耦合 MVC设计模式将 ...
- 在浏览器输入 URL 后会发生什么?超级详细介绍
一个古老的面试问题:当你在浏览器中输入whosmall.com并且按下回车之后发生了什么? 不过我们不再局限于平常的回答,而是想办法回答地尽可能具体,不遗漏任何细节. 这将是一个协作的过程,所以深入挖 ...
- asp.net ListBox 移除操作的思路
ArrayList arrRight = new ArrayList(); protected void ImageButton1_Click(object sender, ImageClickEve ...
- Python学习之模块进程函数详解
今天在看<Beginning Linux Programming>中的进程相关部分,讲到Linux几个进程相关的系统函数: system , exec , fork ,wait . Pyt ...