题目链接

  • 题意:

    输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*)。字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数。也能够看成连续的数,即能够随意加入空格。

    如今有两种操作:1)在任何位置加入随意类型的字符(数字或者星号)    2)交换字符串中的随意两个字符

    求:最少操作多少次,使得得到的串是一个合法的逆波兰式

  • 分析:

    对于n个星号,n+1个数字的字符串,假设将星号都移动到串的末尾。那么一定是合法的

    对于操作1,假设须要插入数字,那么插入到字符串的最前边是最优的

    对于操作2,仅仅可能将星号和数字交换。而且将星号移到了字符串的后边



    那么,先对串进行操作1,使得数字个数不小于星号的个数加一;然后从左到右扫描字符串,假设当前星号前边的数字和星号不满足相应关系(同上)。须要将当前星号与最后一个数字交换;最后,假设字符串最后不是星号。答案加一
  • 注意:

    假设最后得到的串最后不是星号。那么必定答案加一

    假设输入没有星号。那么答案是0
const int maxn = 1100;

char s[maxn];
int n;
vector<int> v;
int main()
{
int T;
RI(T);
while (T--)
{
v.clear();
RS(s);
n = strlen(s);
int num = 0, sig = 0;
REP(i, n)
{
if (s[i] == '*') sig++;
else
{
v.push_back(i);
num++;
}
}
int ans = 0;
int preadd = 0;
if (!sig)
{
ans = 0;
}
else
{
if (num < sig + 1)
{
preadd = sig + 1 - num;
ans += preadd;
} int sig_num = 0; for (int i = 0; i < n; i++)
{
if (s[i] == '*')
{
sig_num++;
if (preadd < sig_num + 1)
{
int sz = v.size();
int id = v[sz - 1];
v.pop_back();
swap(s[i], s[id]); sig_num--;
preadd++;
ans++;
}
}
else preadd++;
} if (s[n - 1] != '*')
ans++;
}
printf("%d\n", ans); }
return 0;
}

2014牡丹江——Known Notation的更多相关文章

  1. 2014牡丹江——Hierarchical Notation

    problemId=5380" style="background-color:rgb(51,255,51)">题目链接 字符串模拟 const int MAXN ...

  2. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

  3. 2014牡丹江K Known Notation

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

  4. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  5. 2014 牡丹江区域赛 B D I

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...

  6. 2014牡丹江区域赛H(特里)ZOJ3826

    Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, stude ...

  7. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)

    The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...

  8. zoj 3820(2014牡丹江现场赛B题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374 思路:题目的意思是求树上的两点,使得树上其余的点到其中一个点的 ...

  9. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

随机推荐

  1. Yum Error Another app is currently holding the yum lock; waiting for it to exit

    Another app is currently holding the yum lock; waiting for it to exit... The other application is: P ...

  2. 获取文本中你须要的字段的 几个命令 grep awk cut tr sed

    1,grep 2,awk 3,cut 4,tr 5,sed 实例1 获取本地IP地址 /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v ine ...

  3. FPGA+DSP SRIO通信(一)——DSP端参数设置(通道)

    FPGA+DSP SRIO通信(一)——DSP端参数设置(通道) 原创 2017年04月19日 18:56:45 标签: SRIO-C66x 1217 经过漫长的探索之后,博主发现关于TI的板子调试和 ...

  4. html 标签 链接

    <a href="http://www.baidu.com">百度</a> <a href="#here">here< ...

  5. 40-语言入门-40-C小加之随机数

    题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=255   15 20 32 40 67 89 300 400   代码: #includ ...

  6. postgresql on centos (sequelize+pg+nodejs):Failed to find PostgresSQL server.Pleast double check your settings

    公司的一个项目,使用的nodejs做服务端,数据库是postgresql,在本地时一切ok,放在centos时,postgresql配置ok,可以远程访问,但是nodejs在centos启动时,就会报 ...

  7. 【转】在Eclipse中使用JUnit4进行单元测试(高级篇)

    http://blog.csdn.net/andycpp/article/details/1329218 通过前2篇文章,您一定对JUnit有了一个基本的了解,下面我们来探讨一下JUnit4中一些高级 ...

  8. chrono

    时间段的表示 tmplate<class Rep,class Period=ratio<1>> class duration; duration类被用来表示时间段的计量器,Re ...

  9. 第一百九十节,jQuery,编辑器插件

    jQuery,编辑器插件 学习要点: 1.编辑器简介 2.引入 uEditor 编辑器(Editor),一般用于类似于 word 一样的文本编辑器,只不过是编辑为 HTML 格式的.分类纯 JS 类型 ...

  10. Servlet Session 跟踪

    HTTP 是一种"无状态"协议,这意味着每次客户端检索网页时,客户端打开一个单独的连接到 Web 服务器,服务器会自动不保留之前客户端请求的任何记录. 但是仍然有以下三种方式来维持 ...