大意:给定后缀表达式, 每次操作可以添加一个字符, 可以交换两个字符的位置, 相邻数字可以看做一个整体也可以分开看, 求合法所需最少操作数.

数字个数一定为星号个数+1, 添加星号一定不会更优.

先判断若星号过多, 直接在最左边添上数字, 遍历过程中若星号还多的话把星号与右侧数字交换.

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n;
char s[N]; void work() {
scanf("%s", s+1);
n = strlen(s+1);
int star = 0, num = 0;
REP(i,1,n) {
if (s[i]=='*') ++star;
else ++num;
}
int left_num = 0, ans = 0;
if (num<=star) {
left_num += star-num+1;
ans += left_num;
}
int now = n;
REP(i,1,n) {
while (i<now&&s[now]=='*') --now;
if (s[i]=='*') {
if (--left_num<1) {
++ans,--now;
left_num+=2;
}
}
else ++left_num;
}
printf("%d\n", ans);
} int main() {
int t;
scanf("%d", &t);
while (t--) work();
}

Known Notation ZOJ - 3829 (后缀表达式,贪心)的更多相关文章

  1. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  2. ZOJ 3829 Known Notation 贪心

    Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...

  3. 【贪心+一点小思路】Zoj - 3829 Known Notation

    借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...

  4. ZOJ - 3829 Known Notation(模拟+贪心)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...

  5. ZOJ 3829 Known Notation --贪心+找规律

    题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...

  6. ZOJ 3829 Known Notation(贪心)题解

    题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...

  7. zoj 3829 Known Notation

    作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...

  8. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  9. ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)

    题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...

随机推荐

  1. HDU 4366 Successor(dfs序 + 分块)题解

    题意:每个人都有一个上司,每个人都有能力值和忠诚值,0是老板,现在给出m个询问,每次询问给出一个x,要求你找到x的所有直系和非直系下属中能力比他高的最忠诚的人是谁 思路:因为树上查询很麻烦,所以我们直 ...

  2. SpringCloud与Consul集成实现负载均衡

    一.背景 SpringCloud微服务目前比较流行,其中大都在使用的服务注册与发现是Eureka,最近研究了Consul的集群搭建,现使用Consul实现服务的负载均衡.其主要拓扑结构如下: 二.Co ...

  3. C# 截取 byte 字节 转字符串

    byte[] byteArray = System.Text.Encoding.Default.GetBytes(content); Byte[] ThisByte = new Byte[1];Buf ...

  4. 常用的 Linux 命令

    列出文件列表:ls [参数 -a -l]创建目录和移除目录:mkdir rmdir用于显示文件后几行内容:tail打包:tar -xvf打包并压缩:tar -zcvf查找字符串:grep显示当前所在目 ...

  5. A successful Git branching model——经典篇

    A successful Git branching model In this post I present the development model that I’ve introduced f ...

  6. Footnotes for tables in latex - 为latex的table加上footnotes

    参考: Footnotes for tables in LaTeX Footnote in tabular environment Footnotes for tables in latex - 为l ...

  7. Terminal run py文件

    cd Documents cd PythonCode python3 hello.py Text Editor: Atom Atom 可以用来写 python 脚本 (文件后缀名 .py). 但是不用 ...

  8. Python学习 day03打卡

    今天学习的主要内容: ppython的基本数据类型: 1. python基本数据类型回顾 2.int---数字类型 4.str---字符串类型 一.python基本数据类型 1. int==>整 ...

  9. hdu 6069 Counting Divisors 筛法

    Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Oth ...

  10. C#端加载数据库,Combobox与Node控件绑定数据源demo示例

    最近一直在做网页.用的js比较多,最近需要做一个C#相关的demo,一开始还有点不适应,写了几句有点感觉了 本篇博客的主要内容是C#怎么读取数据库文件里的数据以及相关控件如何绑定数据源,所做的Demo ...