题意:给一串字符,问你最少几步能变成后缀表达式。后缀表达式定义为,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 ;
}

最新文章

  1. Xamarin.Android之布局文件智能提示问题
  2. linux挂载远程samba目录
  3. django-cms安装
  4. SlickGrid example 3b: 支持撤销操作的编辑单元
  5. hiho_1053_居民迁移
  6. Ubuntu虚拟机与Window、Arm的通信
  7. linksys wrt160nv3 刷dd-wrt固件
  8. 一看就懂ReactJS
  9. [C#技术参考]在PictureBox 中绘图防止闪烁的办法
  10. Cocos2d-x之MenuItem
  11. Luogu P1451 求细胞数量
  12. centos 系统日志
  13. 20160217.CCPP体系详解(0027天)
  14. ActiveMq Windows 配置优化
  15. Windows PowerShell 入門(7)-関数編2
  16. jq 回车提交指定按钮
  17. Identity Server4学习系列一
  18. HTML 脚本 (Script) 实例
  19. Springbatch Miscellanea Notes
  20. 事件冒泡 比bubble

热门文章

  1. idea软件上设置爱彼迎字体
  2. Mockito/PowerMockito Straige Issues
  3. &lt;6&gt;Cocos Creator​​​​​​​调试
  4. steam Depot 生成与应用脚本
  5. 5.JVM的内存区域划分
  6. Spring Security实现RBAC权限管理
  7. Python并发编程之线程池/进程池--concurrent.futures模块
  8. 前端规范--eslint standard
  9. C# 选项卡控件
  10. MongoDB With Spark遇到的2个错误,不能初始化和sample重复的key