ZOJ 3829 Known Notation(贪心)题解
2024-09-13 00:21:20
题意:给一串字符,问你最少几步能变成后缀表达式。后缀表达式定义为,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 ;
}
最新文章
- Xamarin.Android之布局文件智能提示问题
- linux挂载远程samba目录
- django-cms安装
- SlickGrid example 3b: 支持撤销操作的编辑单元
- hiho_1053_居民迁移
- Ubuntu虚拟机与Window、Arm的通信
- linksys wrt160nv3 刷dd-wrt固件
- 一看就懂ReactJS
- [C#技术参考]在PictureBox 中绘图防止闪烁的办法
- Cocos2d-x之MenuItem
- Luogu P1451 求细胞数量
- centos 系统日志
- 20160217.CCPP体系详解(0027天)
- ActiveMq Windows 配置优化
- Windows PowerShell 入門(7)-関数編2
- jq 回车提交指定按钮
- Identity Server4学习系列一
- HTML 脚本 (Script) 实例
- Springbatch Miscellanea Notes
- 事件冒泡 比bubble
热门文章
- idea软件上设置爱彼迎字体
- Mockito/PowerMockito Straige Issues
- <;6>;Cocos Creator​​​​​​​调试
- steam Depot 生成与应用脚本
- 5.JVM的内存区域划分
- Spring Security实现RBAC权限管理
- Python并发编程之线程池/进程池--concurrent.futures模块
- 前端规范--eslint standard
- C# 选项卡控件
- MongoDB With Spark遇到的2个错误,不能初始化和sample重复的key