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);

}

}

时间: 2024-10-08 16:42:23

ZOJ 38727(贪心)的相关文章

zoj 3627(贪心)

思路:半夜了思路有点混乱wa了好几发.一开始坑定两个人距离为m才能获得最大的收益,所以我们就可以枚举单个端点,当距离达到m时在一同一个方向走这是我们只需要算一下剩下几秒,左右两边贪心去最大的即可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2

zoj 1543 贪心

Stripies Time Limit: 2 Seconds      Memory Limit: 65536 KB Our chemical biologists have invented a new very useful form of life called stripies (in fact, they were first called in Russian - polosatiki, but the scientists had to invent an English name

ZOJ - 3715贪心

ZOJ - 3715KindergartenElection 题目大意:幼儿园里正在举办班长选举,除1号小朋友外每个人都会投他最好的朋友,但1号小朋友可以贿赂别人(小伙子有丶想法),被贿赂的小朋友就会把票投给1号小朋友而不是他最好的朋友,对于不同的小朋友贿赂的花费也不同,1号小朋友想要自己是唯一的班长(票数最高),问他最少需要花费多少糖果? 由题目来想,很容易想到贪心,但是不知道怎么贪心.如果是简单让1号是票数最高的小朋友,他每次贿赂都有两种选择,一种是贿赂花费小的人来投他,另一种是贿赂票数比他

Stock (zoj 2921 贪心经典)

Stock Time Limit: 2 Seconds      Memory Limit: 65536 KB Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that allows them to predict wether or not a company will be succesful. This obviously gives them a

ZOJ 3829 贪心 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题,自己智商不够,不敢搞,想着队友智商好,他们搞吧,但是没出来这题...... 以后任何时候,都自信点....该想的还是好好自己想,这类题感觉就是先去找性质,然后一点点找规律,如果必要的话,自己提出一点猜想,然后如果自己举不出来反例,就暂时认为是正确的 下午搞了一下午,发现还是悲剧,晚上参考了两个题解 http://blog.csdn.

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history rolling forward, our king conquered a new region in a distant continent. There are N towns (numbered from 1 to N) in this region connected by s

zoj 3778 Talented Chef 贪心

zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完成所有饼至少需要多少时间. 限制: 1 <= n,m,ti <= 40000 思路: 贪心 ans=max(ceil(sigma(1~n,ti)/m),max(ti)) /*zoj 3778 Talented Chef 题意: 有n个饼,给出完成每个饼所需要的时间t1,t2,...,tn,现在有m个锅(也就是说可以同时煎m个饼),问完