ZOJ 38727(贪心)
这道题真心坑。越想越远 想的飞起来了。 最后纠结起后缀表达式的定义来了.
题意:
就是给你一个串 , 让你用最少改动次数来实它变成一个合法的后缀表达式, 改动方式有两种, 一种是直接加入数字或者*,或者是交换两个字符的位置。
题解:
首先保证星号所须要的数字比当前串的数字大,假设不足,则加入须要数字到字符串首 , 然后从头朝尾扫, 假设当前状态合法则不须要管。当前状态不合法的时候将* 号移到最末尾。然后没了
代码:
#include<stdio.h>
#include<string.h>
int Max(int a, int b)
{
if(a > b) return a;
return b;
}
int main()
{
int T, len;
char s[1005];
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
len = strlen(s);
int num1, num2, Star, Ans, Num;
num1 = num2 = 0;
for(int i = 0; i < len; i++)
if(s[i] == '*') num1 ++;
else num2 ++;
Ans = Max(num1 + 1 - num2, 0);
Star = 0, Num = Ans;
for(int i = 0; i < len; i++)
{
if(s[i] == '*') Star ++;
else Num ++;
if(Star + 1 > Num)
Ans ++, Num ++, Star --;
}
printf("%d\n", Ans);
}
}
转载于:https://www.cnblogs.com/zfyouxi/p/5193006.html
ZOJ 38727(贪心)相关推荐
- zoj 3627(贪心)
思路:半夜了思路有点混乱wa了好几发.一开始坑定两个人距离为m才能获得最大的收益,所以我们就可以枚举单个端点,当距离达到m时在一同一个方向走这是我们只需要算一下剩下几秒,左右两边贪心去最大的即可. 代 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- ZOJ 1025 Wooden Sticks(快排+贪心)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 题目大意:机器运送n个木条,每个木条有一个长度和重量.运送第一根木 ...
- zoj 2709 Lottery 组合数,概率,贪心 (8-F)
题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2709 题解: 1 组合数的计算,用DP,速度又快又简洁. ...
- 【ZOJ - 2968 】Difference Game (贪心,思维模拟)
题干: Now you are going to play an interesting game. In this game, you are given two groups of distinc ...
- 【ZOJ - 3778】Talented Chef(贪心)
题干: As we all know, Coach Gao is a talented chef, because he is able to cook M dishes in the same ti ...
- 【ZOJ - 3946】Highway Project(最短路子图,维护双权值,贪心,最小树形图)
题干: Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he ...
- 【ZOJ - 3963】Heap Partition (STLset,二叉树的性质,构造,贪心,思维)
题干: A sequence S = {s1, s2, ..., sn} is called heapable if there exists a binary tree Twith n nodes ...
- 【ZOJ - 3211】Dream City (01背包类问题,贪心背包)
题干: JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the y ...
最新文章
- ElasticSearch聚合aggs入门
- 设计模式:观察者模式(有利于代码解耦)
- 导出Excel java
- 计算机文献双语外文,中英文双语计算机专业毕业设计外文文献翻译成品:对Delphi的概述.docx...
- sklearn分类器算法:逻辑回归及案例分析
- python写自动化工具_微软最强 Python 自动化工具开源了!不用写一行代码!
- 抓球球的机器人应该怎么玩_王者荣耀:在游戏中当自己优势队友劣势的时候应该怎么玩?...
- Docker垃圾清理方案
- 框架会使程序员变笨吗?
- Fatal Error LNK1123:转换到COFF期间失败:文件无效或损坏
- python实现webrtc接入ipc_WebRTC音视频会议的优势
- MySQL多IDC部署注意事项
- mysql数据库基本知识
- 实用主义学python 下载_麻瓜编程:实用主义学Python(5套全)
- matlabGUI入门
- win10 网卡驱动正常搜索不到WiFi信号 解决方法
- 如何在word中同一位置处插入多篇连续文献
- android陀螺仪方向,android – 如何使用罗盘读数和陀螺仪读数获取手机的方位角?...
- 个人电脑windows装青龙面板,本地运行,无需服务器,本人亲测成功
- ApacheCN 公众号文章汇总 2019.9