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 ...
随机推荐
- 判断请求是不是ajax
public static bool IsAjaxRequest(HttpRequest request) { if (request == null) { throw new ArgumentNul ...
- POJ 2796 Feel Good(单调栈)
传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...
- UVa 11889 Benefit(数论)
题目链接: 传送门 Benefit Time Limit: 5000MS Memory Limit: 32768 KB Description Recently Yaghoub is play ...
- soapUI使用-DataSource获取oracle库中的参数
soapUI使用-DataSource获取oracle库中的参数 下载mysql和oracle驱动包:http://pan.baidu.com/s/1i3sy1MH 放在Program Files\S ...
- 【Beta版本】冲刺-Day3
队伍:606notconnected 会议时间:12月11日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:学习了很多androi ...
- hdu 2010 - 水仙花数
题意: 数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求输出所有在m和n范围内的水仙花 ...
- Base64复习
http://www.cnblogs.com/chengxiaohui/articles/3951129.html
- easyUI的控件
CSS类定义: div easyui-window window窗口样式 属性如下: 1) modal:是否生成模态窗口.tru ...
- css011 表格和表单的格式化
css011 表格和表单的格式化 一. 让表格专司其职 Html中创建一个三行三列的表格 <table> <caption align="bottom" ...
- JS获取select选中的值,所有option值
<select name="myselect" id="myselect"> <option value="2042"&g ...