ZOJ 3829 Known Notation(贪心)题解
题意:给一串字符,问你最少几步能变成后缀表达式。后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格。你可以进行两种操作:加数字,交换任意两个字符。
思路:(不)显然,最终结果数字比*号至少多1,如果缺了数字就直接放到字符串最前面(这样肯定能和后面的*运算),加步数。然后遍历,遇到*号如果当前数字够就直接运算,不够那么就把*和最后面的数字交换。讲一下为什么要可以,因为我们要保证末尾不是数字,如果末尾数字数字显然不是后缀表达式,其次我们交换之后当前的运算就没了,那么就过了,然后后面那个我们换过去的*自有最前面的我们加的数字去运算。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 1e3 + ;
const int seed = ;
const ll MOD = ;
const int INF = 0x3f3f3f3f;
char s[maxn];
int main(){
int t, num = , star = ;
scanf("%d", &t);
while(t--){
scanf("%s", s);
int len = ;
num = star = ;
while(s[len] != '\0'){
if(s[len] == '*') star++;
else num++;
len++;
}
int save = , ans = ;
if(num < star + ){
save = star + - num;
ans += save;
}
int p = , from = len - ;
while(p < len){
if(s[p] != '*'){
save++;
p++;
}
else{
if(save >= ){
save--;
p++;
}
else{
for(int i = from; i > p; i--){
if(s[i] != '*'){
swap(s[i], s[p]);
ans++;
save++;
break;
}
}
p++;
}
}
}
printf("%d\n", ans);
}
return ;
}
ZOJ 3829 Known Notation(贪心)题解的更多相关文章
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
- ZOJ 3829 Known Notation 贪心 难度:0
Known Notation Time Limit: 2 Seconds Memory Limit: 65536 KB Do you know reverse Polish notation ...
- ZOJ 3829 Known Notation --贪心+找规律
题意:给出一个字符串,有两种操作: 1.插入一个数字 2.交换两个字符 问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- zoj 3829 Known Notation
作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...
- 【贪心+一点小思路】Zoj - 3829 Known Notation
借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- Known Notation ZOJ - 3829 (后缀表达式,贪心)
大意:给定后缀表达式, 每次操作可以添加一个字符, 可以交换两个字符的位置, 相邻数字可以看做一个整体也可以分开看, 求合法所需最少操作数. 数字个数一定为星号个数+1, 添加星号一定不会更优. 先判 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
随机推荐
- linux 中的screen出现cannot find terminfo entry 的错误
事情的起因: 本地使用urxvt terminal ,使用ssh方式远程登录服务器,远程在服务器端执行screen命令,然后就出现了cannot find terminfo entry的错误. 解决方 ...
- C# Activator和new的区别
1.你需要动态的创建一个实例模型的时候,就用Activator.CreateInstance(Type type);如果是明确的知道要创建哪个实例的模型,就可以用 new Class1()了. T t ...
- pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 526, saw 5
pandas.io.common.CParserError: Error tokenizing data. C error: Expected 1 fields in line 526, saw 5 ...
- PersistenceContext.properties()
在做 Spring + SpringMVC + SpringData 时,单元测试 报这个错误: java.lang.NoSuchMethodError:javax.persistence.Persi ...
- 按渠道计算 PV 和 UV
按渠道计算 PV 和 UV: ------------------按指定channel_id按月求PV.UV------------ drop table if exists tmp_pvuv; cr ...
- centos 6.5 防火墙开放指定端口
清除防火墙规则:iptables -F 关闭防火墙 /etc/init.d/iptables stop 关闭防火墙开机自启:chkconfig iptables off 查看iptables 是否开 ...
- hibernate二级缓存ehcache hibernate配置详解
<!-----------------hibernate二级缓存ehcache------------------------->hibernate配置 <prop key=&quo ...
- CRM 权限设置 ss
表结构的设计 权限表 url -url地址的正则表达式 ^$ title - 标题 角色表 name - 角色名称 permissions 多对多关联权限表 (权限和角色的关系表) 用户表 name ...
- js获取浏览器信息
function message() { txt = "<p>浏览器代码名: " + navigator.appCodeName + "</p>& ...
- 前端框架VUE----补充
修饰符 .lazy 在默认情况下,v-model 在每次 input 事件触发后将输入框的值与数据进行同步 .你可以添加 lazy 修饰符,从而转变为使用 change 事件进行同步: <!-- ...