2014年ACM牡丹江赛区现场赛K题(ZOJ 3829)
2015-07-31 11:25
375 查看
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 *".
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.
题意:
给一个长度不超过1000的字符串,给出两种操作。
操作1:在任意位置插入任意字符
操作2:交换字符串中的任意两个字符的位置
问:如何用最少的操作将其转换成后缀表达式的形式
解题思路:
首先要明白,字符串中,左边的数字再多,只要右边有一个操作符,那么便算是一个后缀式,
而:在字符串中数字字符个数 大于 操作字符个数时,交换操作总是比插入操作更加划算。
因此,我们只需要在一开始时比较一下两种字符串的大小,如果数字字符数量不能大于操作字符数量,那么把差值补齐在最左边,然后再剩下的字符串中进行交换操作即可。
时间复杂度O(n)
如有BUG,请大家务必指出,不胜感激~
联系方式:274489985@qq.com
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 integerT 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
题意:
给一个长度不超过1000的字符串,给出两种操作。
操作1:在任意位置插入任意字符
操作2:交换字符串中的任意两个字符的位置
问:如何用最少的操作将其转换成后缀表达式的形式
解题思路:
首先要明白,字符串中,左边的数字再多,只要右边有一个操作符,那么便算是一个后缀式,
而:在字符串中数字字符个数 大于 操作字符个数时,交换操作总是比插入操作更加划算。
因此,我们只需要在一开始时比较一下两种字符串的大小,如果数字字符数量不能大于操作字符数量,那么把差值补齐在最左边,然后再剩下的字符串中进行交换操作即可。
时间复杂度O(n)
#include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #include <sstream> #define PI acos(-1.0) #define eps 1e-8 const int inf = (1<<30) - 10; using namespace std; const int maxx = 1000 + 10; int T; int ans; char str[maxx]; int main() { //freopen("input.txt","r",stdin); cin>>T; while(T--) { scanf("%s",str); int t1 = 0; int t2 = 0; int p = strlen(str); for(int i = 0; i < p; ++i) { if(str[i] == '*') { t1++; } else { t2++; } } ans = 0; int l = 0; int lnum = 0; int r = p-1; if(t2<=t1) { lnum = t1 - t2 + 1; ans += lnum; } for(; l <= r; ++l) { if(str[l] == '*') { if(lnum>=2) { lnum--; } else { ans++; while(l <= r) { if(str[r] != '*') { swap(str[l],str[r]); lnum++; r--; break; } r--; } } } else { lnum++; } } printf("%d\n",ans); } return 0; }
如有BUG,请大家务必指出,不胜感激~
联系方式:274489985@qq.com
相关文章推荐
- jquery中这句 .stop(false,true); 什么意思。
- word-ladder 问题
- MySQL的索引类型及简单优化
- iptables 各个参数介绍
- android开源框架(开始开发的时候参考)
- QT foreach 和 QTextCodec
- 《汇编语言》学习笔记 四~六章
- 关于python的编码
- PIL support JPEG
- poj-2318 TOYS
- 标准模式 怪异模式 盒模型 doctype
- CentOS6.5+php5.3听云安装过程
- JQM弹出对话框
- Android关于buildToolVersion与CompileSdkVersion的区别
- 解决 PHP 的 mail() 发送邮件时出现乱码的问题
- Android性能优化之一:ViewStub
- spark机制note
- Android Studio中使用git功能无法clone原因分析
- SSMS插件开发指南
- string类的编写